[英]My python random number generate the same result
所以我在 python 中做这个小游戏并遇到了一个问题。 我想随机化 randomNumber 变量。 每次我跑步时它都会改变,但是当我打印数字时,它会一直显示相同的数字。 例如,我选择玩游戏 3 次,我的游戏会让我输入 3 个值,但随机数对于所有 3 个都是相同的。
代码:
import random
def guessingNumber():
userPlayTimes = int(input('How many times you wanna play : '))
randomNumber = random.randrange(1,10)
userScore = 0
for x in range(1,userPlayTimes+1):
userGuess = int(input('Please enter your random Guess : '))
print(userScore , randomNumber)
if userGuess == randomNumber:
userScore += 1
return userScore
print(userScore)
结果:
Q : How many times you wanna play : 5
Please enter your random Guess : 1
0 3 (score , randomNumber)
Q : Please enter your random Guess : 2
0 3 (score , randomNumber)
Q : Please enter your random Guess : 3
0 3 (score , randomNumber)
1 (overall score)
请不要介意我是否不擅长写作,因为这是我第一次使用 stackoverflow 来提问。
您应该在内部循环中移动您的随机数选择。
目前随机数是在游戏开始之前选择的,并且对于所有游戏都是相同的。
for x in range(1,userPlayTimes+1):
userGuess = int(input('Please enter your random Guess : '))
randomNumber = random.randrange(1,10) # <-- here
print(userScore , randomNumber)
if userGuess == randomNumber:
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.