随时退出测验函数-Python
我试着做一个函数,当在测验中任何时候输入“q”时退出游戏,但它不起作用,它在intcheck函数下,是strchecker,我有另一个strchecker函数,也许这就是它不起作用的原因。我知道它需要打破一个循环,我只是python的初学者随时退出测验函数-Python,python,Python,我试着做一个函数,当在测验中任何时候输入“q”时退出游戏,但它不起作用,它在intcheck函数下,是strchecker,我有另一个strchecker函数,也许这就是它不起作用的原因。我知道它需要打破一个循环,我只是python的初学者 score = 0 def strchecker(question): valid=False while not valid: user_Name = input(question) if user_Nam
score = 0
def strchecker(question):
valid=False
while not valid:
user_Name = input(question)
if user_Name!="":
valid=True
return user_Name
else:
print("Please do not leave username blank")
print("*********************Welcome to the Te Reo Maori Quiz************************\n"
"You will be give a series of 6 questions,\nto answer you will enter an answer between 1 and 4.\n\nBest of Luck,and remember if you would like to quit the game just press enter :)\n")
user_Name = strchecker("Please enter your username:\n")
print("Hi", user_Name,"Here is your first question:\n")
keep_going=""
while keep_going=="":
# List of questions in Quiz
question_List = ["How do you write number 1 in Maori?\n1.Tekau 2.Tahi 3.Ono 4.Rua",
"What is does tahi + tahi = ?\n1.Rua 2.Rimu 3.Ono 4.Tahi",
"How do you write blue in Maori?\n1.Kakariki 2.Kikorangi 3.Whero 4.Ma",
"What two colours make blue?\n1.Ma + Whero 2.Kikorangi + Kowhai 3.Whero + Pararui 4.Ma + Mangu",
"Who was the god of the forest and birds?\n1.Ranginui 2.Paptuanuku 3.Tane-Mahuta 4.Tangaroa",
"Who were Tane Mahutas Parents?\n1.Tangaroa + Ranguinui 2.Punga + Ranganui 3.Tangaroa + Rongo 4.Papatunuku + Ranganui"]
# List of Correct Answers
correct_Answer = [2, 1, 2, 2, 3, 4]
# If user enters anything that is not an integer between 1 and 4 it will be an invalid input
def intcheck(question, low, high):
valid= False
while not valid:
error= "Whoops! Please enter an integer between {} and {}\n".format(low, high)
try:
response = int(input("Please enter your answer or press 'q' to quit"))
if low <= response <= high:
return response
else:
print(error)
print()
except ValueError:
print(error)
def strchecker(question):
valid=False
while not valid:
Quit = input(question)
if user_Name!="q":
valid=True
return user_Name
else:
print("Quit Game")
break
# Get a question from the question list and print and loop one by one
for idx, question in enumerate(question_List):
print(question)
Answer = intcheck("Please enter in an answer or press 'q' to quit", 1,4)
print()
# Get answer and check if it is correct or incorrect by going to the list of correct answers
if Answer == correct_Answer[idx]:
print("Well Done, your answer was correct\n")
score +=1
else:
print("Hard Luck, your answer was incorrect\n")
if score <4:
print("************************************************************")
print("You got",score,"out of 6.\n\nYou should get more than 3/6, try the quiz again to improve your knowledge\n")
print("*************************************************************")
elif score >4 or score<7:
print("*************************************************************")
print("You got",score,"out of 6.\n\nNice job! Your study payed off!\n")
print("*************************************************************")
end = False
while end == False:
response= input("Would you like to play again?\n"
"If yes enter 'yes or 'y' if not enter'n' or 'no' to quit:\n")
if response.lower()=="no" or response.lower()=="n":
print("You have quit the game. Thank you for playing",user_Name,":) ")
keep_going = "end"
break
elif response.lower()=="yes" or response.lower()=="y":
end = True
print("Here is the Te Reo Maori Quiz again",user_Name,"\n")
keep_going
else:
print("**Invalid input please enter 'y' if you want to play again\n or 'n' if you would like to quit game**\n")
score=0
def strchecker(问题):
有效=错误
虽然无效:
用户名=输入(问题)
如果用户名="":
有效=真
返回用户名
其他:
打印(“请不要将用户名留空”)
打印(“****************************欢迎参加Te Reo毛利测验*********************************************\n”
“您将得到一系列6个问题,\n要回答,您将输入一个介于1和4之间的答案。\n\n幸运的是,请记住,如果您想退出游戏,请按enter:)\n”)
user\u Name=strchecker(“请输入您的用户名:\n”)
打印(“您好”,用户名,“这是您的第一个问题:\n”)
继续前进=“”
继续前进时==“”:
#问答游戏中的问题列表
问题列表=[“你如何用毛利语写数字1?\n1.Tekau 2.Tahi 3.Ono 4.Rua”,
“tahi+tahi=”是什么?\n1.Rua 2.Rimu 3.Ono 4.tahi“,
“你怎么用毛利语写蓝色?\n1.Kakariki 2.Kikorangi 3.Whero 4.Ma”,
“哪两种颜色是蓝色的?\n1.Ma+Whero 2.Kikorangi+Kowhai 3.Whero+Pararui 4.Ma+Mangu”,
“谁是森林和鸟类之神?\n1.兰吉努伊2.帕普图阿努库3.塔恩-马胡塔4.坦加拉”,
“Tane Mahutas的父母是谁?\n1.Tangaroa+Ranguiui 2.Punga+Ranganiui 3.Tangaroa+Rongo 4.Papatunuku+Rangani”]
#正确答案清单
正确答案=[2,1,2,2,3,4]
#如果用户输入的任何内容不是介于1和4之间的整数,则该输入无效
def intcheck(问题、低、高):
有效=错误
虽然无效:
error=“哇!请输入一个介于{}和{}之间的整数\n”。格式(低、高)
尝试:
response=int(输入(“请输入答案或按“q”退出”))
如果low一个问题是您在错误的函数中检查出口,那么答案将被输入int checker,而strchecker只给出错误。我已经移动了一些代码,并对其进行了一些简化—仍然可以对代码进行一些清理—它不需要太多for循环。此外,问答数组有点“笨重”——在这里,我将它们全部放入一个二维数组(列表)
另外,我使用sys.exit()
退出,而不是终止外部循环:
import sys
def checkname(name):
valid = False
while not valid:
user_Name = input(name)
if user_Name != "":
valid = True
return user_Name
else:
print("Please do not leave username blank")
def checkans(question, low, high):
valid = False
while not valid:
error = "Whoops! Please enter an integer between {} and {}\n".format(low, high)
response = input("Please enter your answer or press 'q' to quit:\n")
try:
answer = int(response)
if low <= answer <= high:
return answer
else:
print(error)
print()
except ValueError:
if (response == 'q'):
print("Quitting Game")
sys.exit()
else:
print(error)
# List of questions in Quiz -
# EACH QUESTION IS AN ARRAY:
# INDEX 0 IS THE QUESTION
# INDEX 1 IS THE ANSWER
question_List = [["How do you write number 1 in Maori?\n1.Tekau 2.Tahi 3.Ono 4.Rua",2],
["What is does tahi + tahi = ?\n1.Rua 2.Rimu 3.Ono 4.Tahi",1],
["How do you write blue in Maori?\n1.Kakariki 2.Kikorangi 3.Whero 4.Ma",2],
["What two colours make blue?\n1.Ma + Whero 2.Kikorangi + Kowhai 3.Whero + Pararui 4.Ma + Mangu",2],
["Who was the god of the forest and birds?\n1.Ranginui 2.Paptuanuku 3.Tane-Mahuta 4.Tangaroa",3],
["Who were Tane Mahutas Parents?\n1.Tangaroa + Ranguinui 2.Punga + Ranganui 3.Tangaroa + Rongo 4.Papatunuku + Ranganui",4]]
# GAME BEGINS HERE
def playGame():
score = 0
print("*********************Welcome to the Te Reo Maori Quiz************************\n"
"You will be give a series of 6 questions,\nto answer you will enter an answer between 1 and 4.\n\nBest of Luck,and remember if you would like to quit the game just press enter :)\n")
user_Name = checkname("Please enter your username:\n")
print("Hi", user_Name,"Here is your first question:\n")
# Get a question from the question list and print and loop one by one
# LOOP THROUGH QUESTIONS
for idx, question in enumerate(question_List):
print(question[0])
answer = checkans("Please enter in an answer or press 'q' to quit", 1,4)
# Get answer and check if it is correct or incorrect by going to the list of correct answers
if answer == question[1]:
print("Well Done, your answer was correct\n")
score +=1
else:
print("Hard Luck, your answer was incorrect\n")
if score <4:
print("************************************************************")
print("You got",score,"out of 6.\n\nYou should get more than 3/6, try the quiz again to improve your knowledge\n")
print("*************************************************************")
elif score >4 or score<7:
print("*************************************************************")
print("You got",score,"out of 6.\n\nNice job! Your study payed off!\n")
print("*************************************************************")
end = False
while end == False:
response = input("Would you like to play again?\nIf yes enter 'yes or 'y' if not enter'n' or 'no' to quit:\n")
if response.lower() == "no" or response.lower() == "n":
print("You have quit the game. Thank you for playing",user_Name,":) ")
sys.exit()
elif response.lower() == "yes" or response.lower() == "y":
playGame()
else:
print("**Invalid input please enter 'y' if you want to play again\n or 'n' if you would like to quit game**\n")
playGame()
导入系统
def checkname(名称):
有效=错误
虽然无效:
用户名=输入(名称)
如果用户名!="":
有效=真
返回用户名
其他:
打印(“请不要将用户名留空”)
def检查(问题、低、高):
有效=错误
虽然无效:
error=“哇!请输入一个介于{}和{}之间的整数\n”。格式(低、高)
响应=输入(“请输入您的答案或按“q”退出:\n”)
尝试:
应答=int(应答)
如果低