Exiting a questionnaire program after answering incorrectly

Hello everyone,

Thank you for taking the time to check out my first post here. I have been learning Python for a couple of weeks now through Codecademy and earlier today I decided to create a very, very basic program similar to the game show Who Wants to be a Millionaire. I chose to make the questions all related to a video game series I love. The program runs fine despite being incredibly rudimentary. There are lots of sleep pauses for “dramatic effect.” It does not offer lifelines or anything like that. It’s basically just a quiz.

I am having some difficulty understanding how to make the program end when the individual playing answers a question incorrectly. Currently, the program functions through all 15 questions. It prints a message to the user telling them when they answer correctly or incorrectly. However, it then proceeds onto the next question. What is the best way to have the program stop when the user answers incorrectly? Below is an example of how the code looks for each question:

# First question
print("For $100:")
print("Bloodborne released on which platform in which year?\n")
print("A. PC, 2016")
print("B. PlayStation 4, 2015")
print("C. Xbox One, PlayStation 4, and PC, 2015")
print("D. PlayStation 3, 2014\n")
answer1 = "B"
answer1 = str(input("Your guess: "))
time.sleep(1.2)
if answer1 == "B" or answer1 == "b":
  print("\nCorrect!")
else:
  print("\nI'm sorry, that is the wrong answer. You lose!")
time.sleep(0.6)
print("Onto the next question...")
time.sleep(0.6)

Thank you again for your time, and I appreciate any and all help! Take care!

  • You are using the same variable for the stored answer and the user input.
  • input() is a str object; no casting needed.
  • needless logic, if user in “Bb” will do the job.

Okay, great! So I can remove the line

and so on from each question. Thank you!

But, that still doesn’t tell me the answer to my original question. If the correct answer to the question is “b” and the user answers “a,” for example, the program will print “I’m sorry, that is the wrong answer. You lose!” Then it will pause shortly, then print “Onto the next question…” and continue running.

So how do I make the game stop moving forward when the user guesses incorrectly?

Write a small helper function to handle the input. Give it an answer parameter and run a loop inside the function that returns when the answer matches the user input, similar to the following:

def user_input(answer):
    while True:
        user = input("Your answer: ")
    if user in f"{answer.upper()}{answer.lower()}": return user
    print ("Wrong. Try again.")

Excellent, I will give something like this a try. Thank you!

1 Like

Apologies, I’m having a very difficult time wrapping my head around this. Is there no simple way to give a command to end the program if the condition of the if statement is not met?

To quite Python, exit() is an immediate terminator, as in all code execution ceases and the shell is returned to ready state.

>>> help(exit)
Help on Quitter in module _sitebuiltins object:

class Quitter(builtins.object)
 |  Quitter(name, eof)
 |  
 |  Methods defined here:
 |  
 |  __call__(self, code=None)
 |      Call self as a function.
 |  
 |  __init__(self, name, eof)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __repr__(self)
 |      Return repr(self).
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

>>> 

Look for repeated code patterns in the overall program. Much of that can be abstracted away in helper functions and data structures. Code that runs from top to bottom grows in size as more content is added. When a program is modularized into helper functions and data, such as a dictionary or list, tuple or set we can write a control loop for the whole game.

In a control function, all we need to do is return to terminate.

def main():
    # static data
    # helpers
    # game loop
    return "Bye"