如果答案有误,如何使函数重复,所以程序将选择一个新的数字。
进行while循环
import random, sys
def randomNumber(diceRoll):
while True:
print('Pick a number 1 to 6')
userEntry = int(input())
if diceRoll == userEntry:
print('Correct')
sys.exit()
if diceRoll != userEntry:
print('Wrong, try again')
print('The number generated was ' + str(diceRoll))
continue
r = random.randint(1, 6)
outcome = randomNumber(r)
如果用户猜错了,我希望程序再次询问用户随机数,但是使用新的数字。
答案 0 :(得分:0)
如果猜错了,只需重置diceRoll
。
import random, sys
def randomNumber(diceRoll):
while True:
print('Pick a number 1 to 6')
userEntry = int(input())
if diceRoll == userEntry:
print('Correct')
sys.exit()
if diceRoll != userEntry:
print('Wrong, try again')
print('The number generated was ' + str(diceRoll))
diceRoll = random.randint(1, 6)
continue
r = random.randint(1, 6)
outcome = randomNumber(r)