用 python 制作终端游戏,我在 while 循环中遇到问题

问题描述 投票:0回答:1

我不久前开始为一个学校项目制作这个游戏,我想制作一个 while 循环来检查无效答案并再次提出问题,但这样做时我的 if 语句应该简单地回答问题不会,我也不知道该去哪里看。

这是我尝试过的,但每当我在输入中键入 1 或 2 时,代码就会结束。我尝试将 if 语句放入循环中,但没有任何改变

#Add while loop to accept non int answers and recall input.
while True:
    try:
        question1 = int(input("1. oui 2. non \n"))
    except ValueError:
        slow_type("essai un nombre ou de répondre la question.")
        continue
    
    else:
        break

#slow_type the following two strings if condition is met.
if question1 == "1":
    slow_type("Alors, voici mon histoire...")
    
if question1 == "2":
   slow_type("D'accord.")
python if-statement while-loop game-development
1个回答
0
投票

如果我理解你的意图,我认为这就是你需要使用的:

while True:
    try:
        question1 = int(input("1. oui 2. non \n"))
    except ValueError:
        slow_type("essai un nombre ou de répondre la question.")
        continue
  
   #slow_type the following two strings if condition is met.
    if question1 == 1:
        slow_type("Alors, voici mon histoire...")
        
    elif question1 == 2:
       slow_type("D'accord.")
    else:
        break
© www.soinside.com 2019 - 2024. All rights reserved.