【发布时间】:2022-11-23 12:51:02
【问题描述】:
import random
def roll_dice():
dice_drawing = {
1:(
"_________",
"| 1 |",
"| * |",
"----------"
),
2:(
"__________",
"| 2 |",
"| * * |",
"-----------"
),
3:(
"__________",
"| 3 |",
"| * * * |",
"-----------"
),
4:(
"__________",
"| 4 |",
"| * * * * |",
"-----------"
),
5:(
"__________",
"| 5 * |",
"| * * * * |",
"-----------"
),
6:(
"__________",
"| * 6 * |",
"| * * * * |",
"-----------"
)
}
roll = input('Roll the dice Yes/No: ')
while roll.lower() == 'yes'.lower():
dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
print('dice rolled: {} and {}'.format(dice1,dice2))
print("\n".join(dice_drawing[dice1]))
print("\n".join(dice_drawing[dice2]))
roll = input('Roll the dice Yes/No: ')
if roll not in roll:
roll = input('Roll the dice Yes/No: ')
roll_dice()
我无法理解如果用户键入其他内容而不是 yes 或 no,那么我希望迭代再次发生,说无效选项请键入是或否
这段代码工作正常,但是如果用户没有输入 yes 或 no 输入与我希望迭代再次运行不同的关键字,说它是一个无效选项,请输入 yes 或 no,当用户输入错误输入时如何添加它是由是或否定义
【问题讨论】:
-
您可以将
if roll not in roll更改为while roll.lower() not in ('yes', 'no')并在input()调用之前在循环内打印一个说明无效输入的打印
标签: python