[英]My random integer only returns 4
I'm trying to run this code: 我正在尝试运行以下代码:
import random
import time
random_int = random.randrange(1, 6)
time.sleep(.1)
for i in range(1, 101):
print(i)
print("Random: ", random_int)
time.sleep(random_int)
But every time I run it, the random_int always returns the first number it chooses, so: 但是每次我运行它时,random_int总是返回它选择的第一个数字,因此:
1
Random: 4
2
Random: 4
3
Random: 4
4
Random: 4
5
Random: 4
6
Random: 4
7
Random: 4
8
Random: 4
9
Random: 4
10
Random: 4
Process finished with exit code 1 流程以退出代码1完成
Or: 要么:
1
Random: 2
2
Random: 2
3
Random: 2
4
Random: 2
5
Random: 2
6
Random: 2
7
Random: 2
8
Random: 2
9
Random: 2
10
Random: 2
Process finished with exit code 1
Etc. pls help won't let me submit this question without more text so this is text... how's life? 等帮助 下,如果没有更多文字,我将无法提交这个问题,所以这就是文字...生活如何? what ru doing? 你在做什么? don't answer... now wait! 不要回答...现在等待! answer my question but not these questions. 回答我的问题,但不能回答这些问题。
You need to call the random function inside the loop 您需要在循环内调用随机函数
import random
import time
time.sleep(.1)
for i in range(1, 101):
print(i)
random_int = random.randrange(1, 6)
print("Random: ", random_int)
time.sleep(random_int)
What you are doing is printing a variable random_int
which has a value already assigned 您正在做的是打印一个变量random_int
,该变量已经分配了一个值
You have to "roll" your die: 您必须“滚动”自己的骰子:
import random
import time
time.sleep(.1)
for i in range(1, 101):
print(i)
random_int = random.randrange(1, 6)
print("Random: ", random_int)
time.sleep(random_int)
randrange
generates a random, so, if you want to generate a new one each time, you have to call it. randrange
生成一个随机数,因此,如果您想每次生成一个新的随机数,则必须调用它。
I'll answer it because I've been in desperation before 我会回答的,因为我以前很绝望
Going off of @user2357112: 从@ user2357112开始:
You are storing random_int = random.randrange(1, 6) once 您将一次存储random_int = random.randrange(1,6)
The loop is effectively doing nothing but referencing the random_int you stored first. 循环实际上除了引用您首先存储的random_int外什么也不做。
In order to have that change, you need to change the value in random_int, preferably by putting random_int = random.randrange(1, 6) inside the for loop, having it change every time. 为了进行更改,您需要更改random_int中的值,最好是将random_int = random.randrange(1,6)放入for循环中,使其每次都更改。
I hope that clears things up! 我希望这可以清除一切!
The problem is occuring as the assigning line(4th line) is outside the loop. 由于分配行(第4行)在循环外部,因此出现了问题。 You are basically assigning the random_int at first, then printing it again and again without changing the variable. 您基本上是先分配random_int,然后一次又一次地打印它而不更改变量。
import random
import time
random_int = random.randrange(1, 6)
time.sleep(.1)
for i in range(1, 101):
print(i)
print("Random: ", random_int)
time.sleep(random_int)
In order to fix the problem, bring 'random_int = random.randrange(1, 6) ' inside the loop so it assigns the new value every single loops. 为了解决该问题,请将“ random_int = random.randrange(1,6)”放入循环中,以便在每个循环中分配新值。 Like this 像这样
import random
import time
time.sleep(.1)
for i in range(1, 101):
random_int = random.randrange(1, 6)
print(i)
print("Random: ", random_int)
time.sleep(random_int)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.