为什么我已经使用randint函数为它创建了一个对象,但为什么我声明的随机变量没有改变? [英] Why is the random variable I declared not changing even though I have created an object for it using randint function
问题描述
import random
ran = random.randint(000,999)
n = int(input("Enter Any Number = "))
list=[]
for i in range(n):
list.append(ran)
print(i,"----",list)
print(list)
推荐答案
randint()
返回该范围内的随机值.但是调用该函数只会生成一次随机值.因此, ran
包含了调用函数的随机值.您永远不会覆盖 ran
,因此它不会改变.
randint()
returns a random value within that range. But calling that function only generates the random value once. So ran
contains whatever that random value was from where you called the function; you never overwrite ran
so it doesn't change.
展望未来,我建议使用反勾号包围您的代码.此外,这是您应该使用google的一个问题,例如通过搜索"random randint()"来查看该函数的工作原理.
Going forward I suggest using the back ticks to surround your code. Additionally, this is a question you should use google for, e.g. by searching 'random randint()' to see how the function works.
看起来您现在已经格式化了代码块,所以很好.另一个注意事项是,您应避免将Python关键字(例如 list
)用作变量名.这会覆盖Python关键字,如果您想在同一文件中使用各种Python内置函数,可能会很麻烦.
Looks like you formatted your code block now, so that's good. Another note is you should refrain from using Python keywords, such as list
, for variable names. This overwrites the Python keyword which can be troublesome if you want to use various Python built-ins in the same file.
这篇关于为什么我已经使用randint函数为它创建了一个对象,但为什么我声明的随机变量没有改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!