我创建在用户已经猜到1和10我的代码将不会选择一个新的随机数
之间的随机选择号码我的代码工作直至点一个小型项目,其中用户猜测正确的号码。它会回到“重新启动= 1”,但不会生成新的号码?
下面是我的意思是 - https://gyazo.com/1b6c9afc17997e7a0ee059a8b0eeb89e 的屏幕截图正如你在这种情况下看到的那样,数字不会从6变为?
这将是真棒,有任何帮助可能!
# Useful module for selecting random numbers
import random
# Return to this point when user guesses the correct number
restart = 1
while (restart < 10):
# Variable that chooses a random number
Random_Number = (random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
# Loop back to this point
loop = 1
# Checks if users number is correct or not
while (loop < 10):
personStr = input("Guess the random number from 1 - 10?: ")
person = int(personStr)
if person == Random_Number:
print ("You are correct!, the number is", Random_Number)
print ("Try to guess the new number!")
restart = restart + 1
elif Random_Number < person:
print ("Your number is smaller than your selected number, try again!")
loop = loop + 1
else:
print ("Your number is larger than your selected number, try again!")
loop = loop + 1
玩家猜出数字后,你并没有退出内部循环。只需在增加重新启动后添加一个中断。 –