-1

Basically, I am creating this game which consists of the user answering some multiple choice questions. I want it so when the user gets the answer wrong once, they try again and if they get the answer wrong twice, the correct answer shows up. But right now, if the user gets the answer wrong, it skips to the next question. How can I do this? Any help is appreciated. Here is my code:

score = 0
def correct():
  print("Congratulations! You got a point!")
  global score
  score = score + 1
  print("Points: ",score)
  
def incorrect():
  print("Wrong, try again!")
  print("Points: ",score)
  
  
def question(q,a):
  print(q)
  answer = input("Answer: ")
  if answer == a:
    correct()
  else:
    incorrect()

question("What is the capital city of England?\n a. London\n b. Paris\n c. York", 'a')
question("What is the capital city of France?\n a. Rome\n b. Paris\n c. Jersey", 'b')
question("What is the capital city of Germany?\n a. Accra\n b. Weimar\n c. Berlin", 'c')
question("What is the capital city of The Netherlands?\n a. Moscow\n b. Amsterdam\n c. Cape Town", 'b')

2 Answers2

1

Use a for loop. If a correct answer is given, return early from the function.

def question(q,a):
    print(q)
    for attempt in range(2):
        answer = input("Answer: ")
        if answer == a:
            correct()
            return
        else:
            incorrect()
    print("The answer is:", a)

Note: based on the code in the question, the call to incorrect() will always write "try again". You might not want it to print "try again" after a second failed attempt, because the player does not get to try again in that case. You could change this by passing in the attempt number as a parameter to the incorrect function and put a suitable if condition in that function to decide whether to print "try again" or not.

alani
  • 12,573
  • 2
  • 13
  • 23
1

You could modify question function:

def question(q,a):
  print(q)
  answer = input("Answer: ")
  if answer == a:
    correct()
  else:
    print(q)
    answer = input("Answer: ")
    if answer == a:
      correct()
    else:
      incorrect()
Martin Lim
  • 21
  • 3