Python 不正确和正确的功能赢得';行不通
我的分数不会正确打印出来,只是总是说6分之一这个代码在底部,我怎么能让用户按enter键,游戏会在游戏中随时退出,谢谢Python 不正确和正确的功能赢得';行不通,python,python-3.x,function,Python,Python 3.x,Function,我的分数不会正确打印出来,只是总是说6分之一这个代码在底部,我怎么能让用户按enter键,游戏会在游戏中随时退出,谢谢 score = 0 def strchecker(question): valid=False while not valid: user_Name = input(question) if user_Name!="": valid=True return user_Name
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")
# 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?\n:1.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("You got",score,"out of 6.\n\nYou should get more than 3/6, try the quiz again to improve your knowledge.")
elif score >4:
print("You got",score,"out of 6.\n\nYNice job! Your study payed off
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”,
“谁是森林和鸟类之神?\n:1.兰吉努伊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(输入(“请输入您的答案或按enter键退出”))
如果低你有一个简单的打字错误。您在脚本底部附近写了-score=+1
而不是score+=1
。因为enter用于在程序中输入答案,所以最好使用类似“退出游戏类型Y或退出”的命令
检查我的代码
response = str(input("Please enter your answer or press enter to quit\n"))
if response == "":
sys.exit()
else:
response = int(response)
尝试这样的退出游戏
import sys
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")
# 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?\n:1.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 = str(input("Please enter your answer or press enter to quit\n"))
if response == "":
sys.exit()
else:
response = int(response)
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("You got",score,"out of 6.\n\nYou should get more than 3/6, try the quiz again to improve your knowledge.")
elif score >4:
print("You got",score,"out of 6.\n\nYNice job! Your study payed off")
导入系统
分数=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”,
“谁是森林和鸟类之神?\n:1.兰吉努伊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=str(输入(“请输入您的答案或按enter键退出”))
如果响应==“”:
sys.exit()
其他:
响应=int(响应)
如果低