三道题答错后如何结束测验

2024-09-27 20:18:54 发布

您现在位置:Python中文网/ 问答频道 /正文

一旦玩家有三个错误的问题,我的游戏继续而不是停止,我该添加/更改什么,使程序停止,一旦三个问题回答不正确。我在程序中包含了一个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,". 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个,这样你就能更好地理解这个问题。非常感谢你的帮助。你知道吗


Tags: oranswernameinputplayifissleep
1条回答
网友
1楼 · 发布于 2024-09-27 20:18:54

下面是一个完整的工作代码:

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

我把你的问题和答案保存在一个列表中,就像你只是在列表上迭代,你不必为每个问题重复代码(打印和生命检查)。你知道吗

如果你还有任何问题,不要犹豫!你知道吗

相关问题 更多 >

    热门问题