(Python)while循环打破了我的random_number

时间:2017-10-10 12:00:06

标签: python loops random dice

所以我正在做一个项目,它基本上从1到6中选择一个随机数作为迷你项目。

在大多数情况下,它有效。但是当它循环回来时,似乎继续滚动相同的数字。

这是我的意思的截图

enter image description here

正如你所看到的,骰子数量不断滚动。你能看到我的代码出了什么问题吗?

# Useful module for selecting random numbers
import random

# Loop program back to here once user presses anything
loop = 1

#Chooses random number between 1 - 6
Random_Number = (random.choice([1,2,3,4,5,6]))

while (loop < 10):
    #Printing what the user sees
    print ("===============================")
    print ("Your random dice number is:", Random_Number)
    input("Press any key to roll again")
    print ("===============================")

    #looping back to "loop = 1"
    loop = loop + 1

6 个答案:

答案 0 :(得分:2)

在循环之外,您正在生成Random_Number次。 试试这样的事情

while (loop < 10):
    Random_Number = (random.choice([1,2,3,4,5,6]))
    #Printing what the user sees
    print ("===============================")
    print ("Your random dice number is:", Random_Number)
    input("Press any key to roll again")
    print ("===============================")

    loop = loop + 1

答案 1 :(得分:1)

此代码选择一次随机数,然后只打印10次。如果你每次都想要一个不同的随机数,你应该在循环中移动随机选择:

while (loop < 10):
    #Chooses random number between 1 - 6
    Random_Number = (random.choice([1,2,3,4,5,6]))

    #Printing what the user sees
    print ("===============================")
    print ("Your random dice number is:", Random_Number)
    input("Press any key to roll again")
    print ("===============================")

    #looping back to "loop = 1"
    loop = loop + 1

答案 2 :(得分:1)

您需要了解Python(和类似语言)存储值,而不是表达式。如果您写a = 2 + 2,则变量2中没有添加且没有a;那里只有4号。

您的情况完全相同:您认为您将Random_Number定义为旁边表达式的别名,实际上您只存储一个数字。

您当然可以通过在循环中调用random.choice()来解决问题 - 因为已经提出了大约10个答案。但要按照的意思执行操作,请定义一个按指定方式选择数字的函数。每次调用函数时都会执行函数体。

def random_number():
    return random.choice([1,2,3,4,5,6])

while (loop < 10):
    print("you rolled", random_number())
    loop += 1

答案 3 :(得分:0)

只设置一次Random_Number的值,然后在每个循环中显示它。

固定

# Useful module for selecting random numbers
import random

# Loop program back to here once user presses anything
loop = 1

#Chooses random number between 1 - 6
#Random_Number = (random.choice([1,2,3,4,5,6]))

while (loop < 10):
    #Printing what the user sees
    print ("===============================")
    print ("Your random dice number is:", (random.choice([1,2,3,4,5,6])))
    #input("Press any key to roll again")
    print ("===============================")

    #looping back to "loop = 1"

答案 4 :(得分:0)

因为您只生成一次随机数。 它应该是

...
while (loop < 10):
    Random_Number = (random.choice([1,2,3,4,5,6]))
....

也不要用大写字母命名变量,而不是Random_Number使用random_number

答案 5 :(得分:0)

如果您不想在每次迭代时重新定义随机数:

# Useful module for selecting random numbers
import random

# Loop program back to here once user presses anything
loop = 1

#Chooses random number between 1 - 6
Random_Number = lambda : random.choice([1,2,3,4,5,6])

while (loop < 10):
    #Printing what the user sees
    print ("===============================")
    print ("Your random dice number is:", Random_Number())
    print ("===============================")

    #looping back to "loop = 1"
    loop = loop + 1