I'm doing an assignment where I have to conduct a quiz for different topics. This is my code so far.
print("Hello and welcome to Shahaad's quiz!") #Introduction
name = input("What is your name? ")
print("Alright", name,", these will be today's topics:")
print("a) Video Games")
print("b) Soccer")
print("c) Geography")
choice = input("Which topic would you like to begin with?")
if choice == 'video games' or choice == 'Video Games' or choice == 'Video games' or choice == 'a)':print("You picked Video Games.")
print("Question number one:")
print("What is the most popular FPS (First Person Shooter) game?")
print("a) Call of Duty")
print("b) Battlefield")
print("c) Grand Theft Auto 5")
print("d) Counter Strike")
answer = input("Your answer:")
guessesTaken = 0
if answer == 'Call Of Duty' or answer == 'Call of duty' or answer == 'Call of duty' or answer == 'a)' or answer == 'call of duty':print("You are correct!")
else: guessesTaken = guessesTaken + 1print("Incorrect!")print("You have", guessesTaken, "guess left!")
I am trying to make it so that if they get the answer wrong, they get another chance at answering the question. Right now once they get it wrong, they can't type again. Thanks!
You should do as @BartoszKP says, use a while loop to check that the user has entered a valid input.
That being said, I have a few recommendations that might improve the readability of your code.
Instead of this
if choice == 'video games' or choice == 'Video Games' or choice == 'Video games' or choice == 'a)':print("You picked Video Games.")
You could take advantage of the str().lower() method:
if choice.lower() == 'video games' or choice == 'a':print('You picked Video Games.")
The lower() method converts all letters to lower-case.
Regarding the while loop, I don't like to use a flag variables - it adds an extra variable to the code that isn't really needed. Instead, you could make use of break
while True:choice = input('Which topic would you like to begin with?')if choice.lower() == 'video games' or 'a':print('You picked Video Games.')break #This breaks out of the while loop, and continues executing the code that follows the loop
Another solution is to define the choice
variable before the while
loop, and run it until the input is like you want:
choice = input('Which topic would you like to begin with?')
while choice.lower() != 'video games' and choice != 'a':print('Please pick a valid option')choice = input('Which topic would you like to begin with?')
print('You picked "{}".'.format(choice))
If you want to be able to choose between different options, the code could be further improved by checking if the input string is one of the items in a list:
valid_options = ['video games', 'a', 'software', 'b', 'cartoons', 'c']
choice = input('Which topic would you like to begin with?')
while choice.lower() not in valid_options:print('Please pick a valid option')choice = input('Which topic would you like to begin with?')
print('You picked "{}".'.format(choice))
Output:
Which topic would you like to begin with?Movies
Please pick a valid option
Which topic would you like to begin with?vIdeO gaMes
You picked "vIdeO gaMes".Which topic would you like to begin with?software
You picked "software".
If you use Python 2.x, you should also consider using raw_input()
instead of input()
. Please see this related SO question to understand why.