(Python) 无法让用户输入提示另一个测验问题或中断循环
(Python) Trouble making the user input promt another quiz question or break the loop
我是编程新手,请见谅!
***编辑 2:好的,这就是全部内容。一切正常,除了当我试图打破 while enthusiasm is True:
循环时,我只是不断收到越来越多的问题(循环保持 运行)
我正在构建一个 Python 琐事测验,并且我管理了所有的答案输入循环(不允许无效输入,成功地打破了循环)
我希望程序询问用户“你想再问一个问题吗?(y/n)”并在 'n'.
时停止程序
问题是无论我尝试什么,我都会收到更多的琐事问题!
提前致谢
import requests
import pprint
import json
import html
print("Welcome to the ultimate test of knowledge and valour, The Internet Quiz!")
print("You will be given an array of multiple choice questions in general knowledge")
input("Press Enter to start!")
y_n = ["y", "n"]
import random
q_num = 1
score = 0
enthusiasm = True
while enthusiasm is True:
r = requests.get("https://opentdb.com/api.php?amount=1&category=9&type=multiple")
question = json.loads(r.text)
first = question['results'][0]['correct_answer']
second = question['results'][0]['incorrect_answers'][0]
third = question['results'][0]['incorrect_answers'][1]
fourth = question['results'][0]['incorrect_answers'][2]
print("--------------------------------------------------------")
print("Question number " + str(q_num)+ ":")
print(html.unescape(question['results'][0]['question']))
options = [first,second,third,fourth]
random.shuffle(options)
for X in options:
print(html.unescape(X))
legend = (
(options[0], 1),
(options[1], 2),
(options[2], 3),
(options[3], 4)
)
error = False
while error is False:
guess = input("Please enter the number of your answer(1-4):")
try:
guess = int(guess)
except:
print("Your answer must be a number between 1-4.")
continue
if guess <1 or guess > 4:
print("Your answer must be a number between 1-4.")
continue
else:
error = True
if (first, guess) in legend:
score += 1
q_num += 1
print("Correct! \nCurrent score: " +str(score))
acid = True
while acid is True:
yesno=input("Would you like another question? (y/n)")
try:
yesno = str(yesno.lower())
except:
print("Invalid input. Please enter y/n.")
continue
if yesno.lower() != "y" and yesno.lower() != "n":
print("Invalid input. Please enter y/n.")
continue
elif yesno.lower() == "y":
break
else:
acid=False
error=True
continue
else:
print("Incorrect! Better hit the books, buddy! \nCurrent score: " +str(score))
q_num += 1
acid = True
while acid is True:
yesno=input("Would you like another question? (y/n)")
try:
yesno = str(yesno.lower())
except:
print("Invalid input. Please enter y/n.")
continue
if yesno.lower() != "y" and yesno.lower() != "n":
print("Invalid input. Please enter y/n.")
continue
elif yesno.lower() == "y":
break
else:
acid=False
error=True
continue
可以通过以下方式停止程序:
导入系统
sys.exit()
你有两个 while 循环:
while enthusiasm is True:
和
while error is False
让我们看看当你询问用户是否想要另一个问题时会发生什么(稍微简化一点):
yesno=input("Would you like another question? (y/n)")
if yesno.lower() == "y":
break
else:
error=True
您将 error
设置为 True
,但您 未 将 enthusiasm
设置为 False
,因此循环开始再次在顶部。进一步看,enthusiasm
never 被设置为 False
,因此该条件将始终重新开始。简单写
else:
error = True
enthusiasm = False
就好了。我还建议考虑一下您是否 想要 两个循环,以及 enthusiasm
变量的用途是什么。
还有很多其他重构可以在这里完成,但不是你 probably should write
while not error:
而不是明确地检查 error
是否“是”False
。同样,
while enthusiasm:
是对布尔值的良好检查
同样,
yesno=input("Would you like another question? (y/n)")
try:
yesno = str(yesno.lower())
except:
print("Invalid input. Please enter y/n.")
continue
if yesno.lower() != "y" and yesno.lower() != "n":
可以改进。您不需要将 yesno
转换为字符串,因为当它来自 input
时会发生这种情况。您也不需要 try/except(您可能已经从其他地方的 int
转换中获取了它)。由于您已经将 yesno
转换为 lower 一次,因此每次将其与 y
或 n
.
进行比较时,您无需继续这样做
我是编程新手,请见谅!
***编辑 2:好的,这就是全部内容。一切正常,除了当我试图打破 while enthusiasm is True:
循环时,我只是不断收到越来越多的问题(循环保持 运行)
我正在构建一个 Python 琐事测验,并且我管理了所有的答案输入循环(不允许无效输入,成功地打破了循环)
我希望程序询问用户“你想再问一个问题吗?(y/n)”并在 'n'.
时停止程序问题是无论我尝试什么,我都会收到更多的琐事问题! 提前致谢
import requests
import pprint
import json
import html
print("Welcome to the ultimate test of knowledge and valour, The Internet Quiz!")
print("You will be given an array of multiple choice questions in general knowledge")
input("Press Enter to start!")
y_n = ["y", "n"]
import random
q_num = 1
score = 0
enthusiasm = True
while enthusiasm is True:
r = requests.get("https://opentdb.com/api.php?amount=1&category=9&type=multiple")
question = json.loads(r.text)
first = question['results'][0]['correct_answer']
second = question['results'][0]['incorrect_answers'][0]
third = question['results'][0]['incorrect_answers'][1]
fourth = question['results'][0]['incorrect_answers'][2]
print("--------------------------------------------------------")
print("Question number " + str(q_num)+ ":")
print(html.unescape(question['results'][0]['question']))
options = [first,second,third,fourth]
random.shuffle(options)
for X in options:
print(html.unescape(X))
legend = (
(options[0], 1),
(options[1], 2),
(options[2], 3),
(options[3], 4)
)
error = False
while error is False:
guess = input("Please enter the number of your answer(1-4):")
try:
guess = int(guess)
except:
print("Your answer must be a number between 1-4.")
continue
if guess <1 or guess > 4:
print("Your answer must be a number between 1-4.")
continue
else:
error = True
if (first, guess) in legend:
score += 1
q_num += 1
print("Correct! \nCurrent score: " +str(score))
acid = True
while acid is True:
yesno=input("Would you like another question? (y/n)")
try:
yesno = str(yesno.lower())
except:
print("Invalid input. Please enter y/n.")
continue
if yesno.lower() != "y" and yesno.lower() != "n":
print("Invalid input. Please enter y/n.")
continue
elif yesno.lower() == "y":
break
else:
acid=False
error=True
continue
else:
print("Incorrect! Better hit the books, buddy! \nCurrent score: " +str(score))
q_num += 1
acid = True
while acid is True:
yesno=input("Would you like another question? (y/n)")
try:
yesno = str(yesno.lower())
except:
print("Invalid input. Please enter y/n.")
continue
if yesno.lower() != "y" and yesno.lower() != "n":
print("Invalid input. Please enter y/n.")
continue
elif yesno.lower() == "y":
break
else:
acid=False
error=True
continue
可以通过以下方式停止程序:
导入系统
sys.exit()
你有两个 while 循环:
while enthusiasm is True:
和
while error is False
让我们看看当你询问用户是否想要另一个问题时会发生什么(稍微简化一点):
yesno=input("Would you like another question? (y/n)")
if yesno.lower() == "y":
break
else:
error=True
您将 error
设置为 True
,但您 未 将 enthusiasm
设置为 False
,因此循环开始再次在顶部。进一步看,enthusiasm
never 被设置为 False
,因此该条件将始终重新开始。简单写
else:
error = True
enthusiasm = False
就好了。我还建议考虑一下您是否 想要 两个循环,以及 enthusiasm
变量的用途是什么。
还有很多其他重构可以在这里完成,但不是你 probably should write
while not error:
而不是明确地检查 error
是否“是”False
。同样,
while enthusiasm:
是对布尔值的良好检查
同样,
yesno=input("Would you like another question? (y/n)")
try:
yesno = str(yesno.lower())
except:
print("Invalid input. Please enter y/n.")
continue
if yesno.lower() != "y" and yesno.lower() != "n":
可以改进。您不需要将 yesno
转换为字符串,因为当它来自 input
时会发生这种情况。您也不需要 try/except(您可能已经从其他地方的 int
转换中获取了它)。由于您已经将 yesno
转换为 lower 一次,因此每次将其与 y
或 n
.