我怎样才能使功能重复?

霍洛威96

如果答案错误,如何使函数重复,因此程序会选择一个新数字。

做一个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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章