一旦三个问题回答错误,如何结束测验

时间:2019-05-17 04:06:26

标签: python python-3.x

一旦玩家有三个错误的问题,我的游戏就会继续而不是停止,一旦错误回答了三个问题,我将添加/更改使程序停止的内容。我在程序中加入了while循环,我做得不好吗?

UITableView

如果回答了3个问题而不是停止,游戏将继续。我的测验中包含20个问题,但我已附上4个问题,因此您可以更好地看到问题。非常感谢您的提前帮助。

1 个答案:

答案 0 :(得分:0)

这是有效的完整代码:

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

我将您的问题和答案保存在列表中,就像您只是遍历列表一样,您不必为每个问题重复代码(带有打印件和生活检查)。

如果您还有其他问题,请不要犹豫!

相关问题