Python 三道题答错后如何结束测验
一旦玩家有三个问题回答错误,我的游戏将继续而不是停止,当三个问题回答错误时,我应该添加/更改什么使程序停止。我在程序中包含了一个while循环,我没有正确地完成它吗Python 三道题答错后如何结束测验,python,python-3.x,Python,Python 3.x,一旦玩家有三个问题回答错误,我的游戏将继续而不是停止,当三个问题回答错误时,我应该添加/更改什么使程序停止。我在程序中包含了一个while循环,我没有正确地完成它吗 print ("Note: you must answer using A, B, C or D") print (" ") lives = 3 print ("lives =", lives) print (" ") name = input ("What is your name?") print ("Hello",name
print ("Note: you must answer using A, B, C or D")
print (" ")
lives = 3
print ("lives =", lives)
print (" ")
name = input ("What is your name?")
print ("Hello",name,". Good Luck.")
while lives != 0:
##is the player ready to start
play = input ("Would you like to start? (Type Y for yes and N for no)")
if play == "Y" or play == "y":
from time import sleep
sleep (1.0)
print("Starting in...")
sleep (1.0)
print("3")
sleep(1.0)
print("2")
sleep(1.0)
print("1")
break
##3, 2, 1 countdown added wk.4 friday
elif play == "N" or play == "n":
print ("End")
break
else:
print ("That is not an answer.\n")
## 1st Question
question1 = ("1. What is Brisbanes AFL team called?")
options1 = (" a. Brisbane Tigers \n b. Brisbane Lions \n c. Brisbane Broncos \n d. Brisbane Magpies")
print (question1)
print (options1)
answer = input (">")
if answer == "B" or answer == "b":
print ("Correct!")
else:
print("Incorrect.")
print ("lives =", lives - 1)
lives=lives-1
## 2nd Question
question2 = ("2. What sport did Greg Inglis play")
options2 = (" a. rugby league \n b. rugby union \n c. AFL \n d. Soccer")
print (question2)
print (options2)
answer = input (">")
if answer == "A" or answer == "a":
print ("Correct!")
else:
print("Incorrect.")
print ("lives =", lives - 1)
lives=lives-1
## 3rd Question
question3 = ("3. Where were the 2018 Commonwealth Games held?")
options3 = (" a. Sunshine Coast \n b. Melbourne \n c. Brsbane\n d. Gold coast")
print (question3)
print (options3)
answer = input (">")
if answer == "D" or answer == "d":
print ("Correct!")
else:
print("Incorrect.")
print ("lives =", lives - 1)
lives=lives-1
## 4th Question
question4 = ("4. How many players in a netball team can shoot?")
options4 = (" a. 1 \n b. 2 \n c. 3\n d. 4")
print (question4)
print (options4)
answer = input (">")
if answer == "B" or answer == "b":
print ("Correct!")
else:
print("Incorrect.")
print ("lives =", lives - 1)
lives=lives-1
当3个问题回答错误时,游戏继续进行,而不是停止。我的测验包含20个问题,但我附上了4个问题,这样你就能更好地理解这个问题。非常感谢您的帮助。这里有一个完整的工作代码:
from time import sleep
# List that saves questions and answers as in: (question, options, answer)
QUESTIONS = [
("1. What is Brisbanes AFL team called?", " a. Brisbane Tigers \n b. Brisbane Lions \n c. Brisbane Broncos \n d. Brisbane Magpies", "B"),
("2. What sport did Greg Inglis play", " a. rugby league \n b. rugby union \n c. AFL \n d. Soccer", "A"),
("3. Where were the 2018 Commonwealth Games held?", " a. Sunshine Coast \n b. Melbourne \n c. Brsbane\n d. Gold coast", "D"),
("4. How many players in a netball team can shoot?", " a. 1 \n b. 2 \n c. 3\n d. 4", "B")
]
print("Note: you must answer using A, B, C or D")
name = input("What is your name? ")
print(f"Hello {name}. Good Luck.")
def answer_question(question, options, correct_answer):
""" Returns True if the input is correct; False, otherwise"""
print(question)
print(options)
answer = input("> ")
if answer.upper() == correct_answer:
print("Correct!")
return True
print("Incorrect.")
return False
def start_quiz():
"""
Start quiz with full lives.
Returns True when the game is over, if either the user won or it
does not want to keep playing; returns False otherwise.
"""
lives = 3
print(f"You have {lives} lives.")
##is the player ready to start
while True:
play = input("Would you like to start? (Type Y for yes and N for no) ")
if play.upper() == "Y":
sleep (1.0)
print("Starting in...")
sleep (1.0)
print("3")
sleep(1.0)
print("2")
sleep(1.0)
print("1")
break
##3, 2, 1 countdown added wk.4 friday
elif play.upper() == "N":
print (f"Goodbye, {name}!")
return True
print ("That is not an answer.\n")
for question, options, answer in QUESTIONS:
if not answer_question(question, options, answer):
lives=lives-1
if lives == 0:
print("You are out of lives!\nGAME OVER\n")
return False
print(f"{name}, you have answered all questions correctly.\nCONGRATULATIONS!\n")
return True
# Runs the quiz until someone wins or the user is not ready
while True:
result = start_quiz()
if result:
break
我将您的问题和答案保存在一个列表中,就像您只需在列表上迭代,而不必为每个问题重复代码(使用打印和生命检查)
如果你还有任何问题,不要犹豫 你发布的代码没有任何问题,你能解释一下错误的来源吗@lexI想添加一个lifes功能。因此,玩家从三条生命开始,在一个问题回答错误后,他们失去了一条生命。当三条生命都错了,游戏就结束了。我已尝试添加这些代码,但它们不起作用,或者可能是我将它们添加到了错误的位置?请更新完整的代码以及您为实现
lifes
scenarion@lexI意指添加您为您的生活场景所尝试的组合代码,不是像@lexI这样的两个单独的部分没有看到您在代码中使用lifes