[英]Defining variable using value of another variable in Python?
I am trying to get Python to define two integer variables, numberone and numbertwo, with the first being between 1 and 100 and the second being less than the first. 我正在尝试让Python定义两个整数变量numberone和numbertwo,第一个在1到100之间,第二个小于第一个。
I have tried this code: 我已经试过这段代码:
numberone=random.randint(0,100)
numbertwo=random.randint(0,numberone)
but the second number is occasionally larger than the first. 但是第二个数字有时大于第一个。
Am I missing something completely obvious? 我是否缺少一些显而易见的东西? :s
:■
I believe you are confusing "greater than" and "not less than" - is not possible for the numbertwo > numberone
but it might very well be that numbertwo == numberone
, which also means that numbertwo < numberone
is false. 我相信您会混淆“大于”和“不小于”-对于
numbertwo > numberone
是不可能的,但很可能是numbertwo == numberone
,这也意味着numbertwo < numberone
是错误的。
random.randint(a, b)
is a bit confusing in that unlike range(a, b)
which includes numbers up to b - 1
only, randint
might produce b
. random.randint(a, b)
有点令人困惑,因为range(a, b)
不包括仅包含b - 1
数字, randint
可能会产生b
。 To get the "including a
but excluding b
behaviour, use random.randrange
: 要获得“包括
a
但不包括b
行为,请使用random.randrange
:
numberone = random.randint(0, 100) # 0 .. 100
numbertwo = random.randrange(0, numberone) # 0 .. numberone - 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.