Python 如何将随机数分配给变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14381091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:15:06  来源:igfitidea点击:

How do I Assign a random number to a variable?

pythonrandom

提问by user1987349

Before I say anything else, I would like to mention that I am almost completely new to coding, and only have a very rudimentary understanding of python.

在我说其他任何东西之前,我想提一下,我对编码几乎是完全陌生的,并且对 python 只有非常初级的了解。

Now that that's out of the way, my problem is that I am (attempting) to code a text adventure game, along the lines of D&D. I'm stuck at an early stage- namely, how to assign a random integer between 1 and 18 to a variable.

既然那已经不碍事了,我的问题是我(试图)沿着 D&D 的路线编写一个文本冒险游戏。我被困在早期阶段 - 即如何将 1 到 18 之间的随机整数分配给变量。

I've seen some ways of doing this, but the value of the variable changes each time it's called up. This can't happen. The reason for this is because I want the stats (Strength, Wisdom, Intelligence, Dexterity, Charisma, and Constitution) to be a randomly generated but fixed number, that can be called upon and have it the same each time.

我已经看到了一些这样做的方法,但是每次调用时变量的值都会改变。这不可能发生。这样做的原因是因为我希望统计数据(力量、智慧、智力、敏捷、魅力和体质)是一个随机生成但固定的数字,每次都可以调用并保持相同。

I've tried mucking around with things like Str = random.randomint(1,18), using the random module.

我试过Str = random.randomint(1,18)使用随机模块处理诸如 之类的事情。

The closest I've come is using the lambda function, so that when I call up the variable it generates a random number which is different each time. None of these have rally worked, and I would really like to know what I'm doing wrong.

我最接近的是使用 lambda 函数,因此当我调用变量时,它会生成一个每次都不同的随机数。这些都没有奏效,我真的很想知道我做错了什么。

回答by Inbar Rose

I see you are very close with what you have:

我看到你非常接近你所拥有的:

Str = random.randomint(1,18)should be Str = random.randint(1,18)

Str = random.randomint(1,18)应该 Str = random.randint(1,18)

This is the line which assigns the random int to the variable Str, and when you ask for Stryou should get the same number each time.

这是将随机 int 分配给变量的行Str,当您要求时,Str每次都应该得到相同的数字。

If you keep calling Str = random.randint(1,18)it will change Streach time. so only do it once.

如果你一直打电话,Str = random.randint(1,18)Str每次都会改变。所以只做一次。

If you know or understand about classesyou should use them to contain your characters/items/spells/adventures/monsters etc. which can have properties and attributes and inventories etc etc, much like in the game itself, you group all the things under different types.

如果您知道或了解classes您应该使用它们来包含您的角色/物品/法术/冒险/怪物等,它们可以具有属性和属性和库存等,就像在游戏本身中一样,您可以将所有事物分组为不同类型.

Note: You mention you used lambda, that might mean that you have assigned Strto a function which returns a random variable each time, meaning each time you asked for Stryou would actually be asking for the function to return a random number.

注意:您提到您使用了 lambda,这可能意味着您分配Str给了一个每次都返回一个随机变量的函数,这意味着每次您要求Str您实际上都要求该函数返回一个随机数。

回答by imrobertjames

Welcome to coding.

欢迎编码。

I've been a web developer for a while and I've also recently started to sink my teeth into better things than php (in my opinion). I would also use:

我已经做了一段时间的网络开发人员,最近我也开始专注于比 php 更好的东西(在我看来)。我也会使用:

Str = random.randomint(1,18)

Are you using an IDE? I found they're great when you're first learning a new language due to the code completion. It really helps you get to know your way around the language faster, due to not having to go to various websites to look up syntax. Good luck! Text based adventure games are a great way to start learning!

你用的是IDE吗?我发现当你第一次学习一门新语言时,由于代码完成,它们很棒。它确实可以帮助您更快地了解该语言,因为您不必去各种网站查找语法。祝你好运!基于文本的冒险游戏是开始学习的好方法!