python - 如何循环这个问题直到它的答案是正确的
问题描述
如果他们在回答“你想再玩一次”时说“不”,我该如何让它再次循环,这样如果他们说“不”,它会继续循环回到那个问题,直到他们说“是”?
import random
def main():
n = random.randint(3,10)
guess = int(input(" So "+ name +" guess a number from 3 to 10: "))
while n != "guess":
print
if guess < n:
print("Sorry "+ name +" but, that guess is low")
guess = int(input("Guess a number from 3 to 10: "))
elif guess > n:
print("Come on "+ name +" that guess is high")
guess = int(input("Guess a number from 3 to 10: "))
else:
print("Congratulations "+ name+" you guessed it!")
break
restart = input("Do you want to play again: ").lower()
if restart==("yes"):
main()
elif restart ==("no"):
print("Are you sure about that?")
restart = input("Do you want to play again: ").lower()
main()
解决方案
尝试这个:
while True:
restart = input("Do you want to play again: ").lower()
if restart == "yes":
break
elif restart == "no":
print("Are you sure about that?")
main()
推荐阅读
- javascript - 使用 getElementById 时添加类
- r - R Shiny - 为 DataTables 选择扩展 - 获取选定的行并预选行
- bash - 从 Linux 脚本标准输入读取导出命令
- r - 使用哪个函数在条件下转置部分列
- php - 如何使用 PHP 正确计算 Oracle 中的行数?
- c# - 在节点中找不到密钥
- python - 如何使用python从sofascore中抓取足球结果
- angular - 如何创建订阅 BehaviorSubject 和差异类型的返回值
- php - PHP 7.3 Laravel 数据库自动数字舍入错误
- javascript - 在javascript中的html模板字符串中将所有没有http url替换为http://?