需要帮助修复随机数
Need help fixing random number
我正在学习 Python 并且正在研究随机掷骰子。当我 运行 它时,它会重复在询问您是否要再次播放后首次显示的相同数字。我需要帮助找出我哪里出错了。
我试过移动代码并放置不同种类的代码。我只是被难住了。
import sys
import random
import time
greeting = "Welcome to my Dice Game!"
roll = "Lets roll this die!"
die = random.randint(0, 6)
print(greeting)
time.sleep(2)
answer = input("Want to play?")
while answer == "yes" or answer == "y":
print(roll)
time.sleep(2)
print(die)
answer = input("Want to play again?")
print("Thanks for playing!")
这是我得到的:
Welcome to my Dice Game!
Want to play?yes
Lets roll this die!
5
Want to play again?yes
Lets roll this die!
5
Want to play again?y
Lets roll this die!
5
您需要在循环中每次重新计算骰子的值,例如:
import sys
import random
import time
greeting = "Welcome to my Dice Game!"
roll = "Lets roll this die!"
print(greeting)
time.sleep(2)
answer = input("Want to play?")
while answer == "yes" or answer == "y":
print(roll)
time.sleep(2)
die = random.randint(0, 6) # recompute it here instead
print(die)
answer = input("Want to play again?")
print("Thanks for playing!")
当您 运行 命令 die = random.randint(0, 6)
时,您告诉 Python 的是 "Use the random.randint()
function to pick a random integer between 1 and 6, and then set the variable named die
equal to the integer that got chosen"。完成后,您的其余代码不会执行任何操作来更新 die
的值。这意味着循环内的 print(die)
将继续打印它最初给出的任何值。换句话说,命令 die = random.randint(0, 6)
并不 意味着 "Re-run the command random.randint(0, 6)
and get another random number each and every time I refer to die
"。相反,die
只是一些具有特定常量值的变量。
由于 random.randint()
是实际生成数字的原因,因此保持更新 die
的一种方法是简单地将循环外的命令移到循环内:
while answer == "yes" or answer == "y":
print(roll)
die = random.randint(0, 6) # Generate a new random number, then assign it to 'die'
time.sleep(2)
print(die)
answer = input("Want to play again?")
事实上,如果你除了打印数字之外没有对数字做任何事情,你可以完全忘记使用变量,只需将 random.randint()
命令粘贴到 print
命令中:
while answer == "yes" or answer == "y":
print(roll)
time.sleep(2)
print(random.randint(0, 6))
answer = input("Want to play again?")
我正在学习 Python 并且正在研究随机掷骰子。当我 运行 它时,它会重复在询问您是否要再次播放后首次显示的相同数字。我需要帮助找出我哪里出错了。
我试过移动代码并放置不同种类的代码。我只是被难住了。
import sys
import random
import time
greeting = "Welcome to my Dice Game!"
roll = "Lets roll this die!"
die = random.randint(0, 6)
print(greeting)
time.sleep(2)
answer = input("Want to play?")
while answer == "yes" or answer == "y":
print(roll)
time.sleep(2)
print(die)
answer = input("Want to play again?")
print("Thanks for playing!")
这是我得到的:
Welcome to my Dice Game!
Want to play?yes
Lets roll this die!
5
Want to play again?yes
Lets roll this die!
5
Want to play again?y
Lets roll this die!
5
您需要在循环中每次重新计算骰子的值,例如:
import sys
import random
import time
greeting = "Welcome to my Dice Game!"
roll = "Lets roll this die!"
print(greeting)
time.sleep(2)
answer = input("Want to play?")
while answer == "yes" or answer == "y":
print(roll)
time.sleep(2)
die = random.randint(0, 6) # recompute it here instead
print(die)
answer = input("Want to play again?")
print("Thanks for playing!")
当您 运行 命令 die = random.randint(0, 6)
时,您告诉 Python 的是 "Use the random.randint()
function to pick a random integer between 1 and 6, and then set the variable named die
equal to the integer that got chosen"。完成后,您的其余代码不会执行任何操作来更新 die
的值。这意味着循环内的 print(die)
将继续打印它最初给出的任何值。换句话说,命令 die = random.randint(0, 6)
并不 意味着 "Re-run the command random.randint(0, 6)
and get another random number each and every time I refer to die
"。相反,die
只是一些具有特定常量值的变量。
由于 random.randint()
是实际生成数字的原因,因此保持更新 die
的一种方法是简单地将循环外的命令移到循环内:
while answer == "yes" or answer == "y":
print(roll)
die = random.randint(0, 6) # Generate a new random number, then assign it to 'die'
time.sleep(2)
print(die)
answer = input("Want to play again?")
事实上,如果你除了打印数字之外没有对数字做任何事情,你可以完全忘记使用变量,只需将 random.randint()
命令粘贴到 print
命令中:
while answer == "yes" or answer == "y":
print(roll)
time.sleep(2)
print(random.randint(0, 6))
answer = input("Want to play again?")