我怎样才能使功能重复?
How can I make the function repeat?
如果答案错误,我怎样才能让函数重复,所以程序会选择一个新数字。
制作一个 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)
我希望程序在用户猜错时再次询问用户随机数,但使用新的数字。
如果猜错了就重新设置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)
如果答案错误,我怎样才能让函数重复,所以程序会选择一个新数字。
制作一个 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)
我希望程序在用户猜错时再次询问用户随机数,但使用新的数字。
如果猜错了就重新设置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)