python - 如何使功能重复?
问题描述
如果答案错误,如何使函数重复,因此程序选择一个新数字。
制作一个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)
推荐阅读
- python - 将数据集从 google cloud datalab 导出到 google colab
- sql - 基于历史时间序列的统计计算数据库模型
- reactjs - StyledComponents + 打字稿
- azure-data-factory - ADF - 如何通过传递的日期参数过滤器迭代每个日期层次结构文件夹文件
- python - Pandas 函数 read_html 不编码宏字符
- jquery - 无法通过 Jquery 从按钮元素获取数据属性
- ios - RealmSwift 10.7.2 与 xCode 12.4 编译器和构建错误
- mapsui - MyLocation 符号可以用用户图形更改吗?
- r - 使用 igraph R 包与图进行比较时,如何比较两个图以识别图 2 中唯一边的数量
- r - 如何在 R 数据框中删除标题为 NA 的列/变量?