-1

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 + 1
    print("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!

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shahaad Boss
  • 1
  • 1
  • 2
  • 6

2 Answers2

1

This is a simple and quite often encountered problem. The solution usually fits this scenario:

flag = False

while not flag:
    x = input("Get input from the user:")

    if validate(x):
        flag = True
    else:
        print "Input invalid. Try again"

Where variable names should be of course changed to be suitable to the current task (e.g. flag --> answerCorrect or similar, x --> answer etc.).

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
  • In my situation, where would I have to insert while? I am kind of confused right now. – Shahaad Boss Oct 28 '13 at 00:34
  • 1
    @ShahaadBoss Just stop for a moment and try to think it through slowly. Loops repeat behavior. What needs to be repeated in your case? Everything that needs to be repeated should be within a loop. You need to ask a question multiple times, and validate it multiple times. So, at least asking the question and validating it should be in a loop. After that, always check carefully if there is a possibility for the loop to finish (in this case: does the `flag` has a chance to be set to `True` so the loop won't be infinite). To make things simpler consider partitioning your code into functions. – BartoszKP Oct 28 '13 at 00:37
1

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.

Community
  • 1
  • 1
Steinar Lima
  • 7,644
  • 2
  • 39
  • 40