当输入n或no时,测验不会中断,它只会像输入y或yes时一样再次循环测验。(yes\u no函数的代码在底部)我只需要代码方面的帮助谢谢 当输入n或no时,测验不会中断,它只会像输入y或yes时一样再次循环测验。(yes\u no函数的代码在底部)我只需要代码方面的帮助谢谢
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 enter to quit\n"))
if low <= response <= high:
return response
else:
print(error)
print()
except ValueError:
print(error)
# 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 enter 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\n")
elif score >4 or score<7:
print("*************************************************************")
print("You got",score,"out of 6.\n\nNice job! Your study payed off!\n\n")
def strchecker(question):
valid=False
while not valid:
response=input(question)
if response.lower()=="yes" or response.lower()=="y":
response="yes"
valid=True
return response
keep_going
elif response.lower()=="no" or response.lower()=="n":
response="no"
valid=True
return response
print(" Thank you for playing. You have chose to quit the game")
break
else:
print("Invalid input, please enter yes or no\n\n")
yes_No = strchecker("Would you like to play again\n")
您必须知道,当函数到达
return
语句时,它就结束了。例如,如果在return语句之后打印某个内容,它将永远不会被打印好吧。这有很多问题脚本。首先,我觉得自己是第一个strchecker()函数。没有正当的理由存在,因为它只被正确地使用过一次。第二,您不需要仅仅为了使循环无限地进行而分配一个变量,直到满足一个条件(这是您的脚本中最缺少的)。你知道吗
介绍:While True语法:
这里的else-pass意味着只要转到循环的顶部并继续,这通常是隐含的,您不需要在while-True循环中显式地编写else-pass部分。你知道吗
这里是你的脚本的一个修改版本,它使用了这个概念,我已经替换了大多数我认为效率低下的东西,并且改变了代码的动态性,但是我相信本质仍然存在。你知道吗
注:variablec是一个计数器,variableinp代表输入并存储最终的yes/no输入,分数是计算出来的,只是简单地打印出来,而不是str操作和格式化(我相信你可以自己做),为了更容易遵循PEP8标准(例如:将问题列表拆分为问题列表和选项列表),我们进行了一些更改。你知道吗
相关问题 更多 >
编程相关推荐