0

I am doing an assignment where I have to conduct a quiz. Here 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:")  #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)' or choice == 'a':
    print("You picked Video Games.")
print("Question number one:")                                      #Question 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")       
maxGuesses = 2 #Max number of attempts for the problem
guessesTaken = 0
points = 0
question = input("Your answer: ")
if question == 'Call of duty' or question == 'call of duty' or question == 'Call Of Duty' or question == 'Call of Duty' or question == 'a' or question == 'a)':
    print("You are correct! You guessed the question on the first try!")
    points = points + maxGuesses-guessesTaken
    print("You scored",(maxGuesses-guessesTaken), "points!")
else:
    print("Incorrect!")
    print("You have", (maxGuesses-guessesTaken-1), "guesses remaining!")
    answerb = input("Your answer: ")
if answerb == 'Call of duty' or answerb == 'call of duty' or answerb == 'Call Of Duty' or answerb == 'Call of Duty' or answerb == 'a' or answerb == 'a)':
    print("You are correct!")
    points = points + maxGuesses-guessesTaken
    print("You scored", (maxGuesses-guessesTaken-1), "points!")

The problem i'm having is on line 28, where it says answerb is not defined but i did define it. I am supposed to do this quiz with terms that I have learnt and I am not allowed to use terms i haven't learnt such as while. I put else: print incorrect and put answerb == input to give the user a 2nd chance at answering. And if they get it the first try, they dont need to insert something for answerb.

Shahaad Boss
  • 1
  • 1
  • 2
  • 6
  • 1
    Mate, this is the third question you ask on SO about elementary mistake in your program. You get the mistakes because your program is hard to read and lacks proper structure. You've already got a couple of good answers here: http://stackoverflow.com/questions/19625407/python-progamming-loop Use them. I assure you, you'll thank yourself for doing it. Spend some time on learning programming logic and it will help you later. – sashkello Oct 29 '13 at 01:48
  • I apologize but I tried all the edits and answers they gave me nothing worked. I always received the same error and/or different one. I am studying programming language, and I am a beginner at this. I have only recently taken the course. – Shahaad Boss Oct 29 '13 at 01:51
  • Well, start with something simple. Rewrite program with these two questions so that you have one attempt for each of them. Store questions in array and loop through them. When it works start adding features, such as number of attempts, points calculation etc. Don't try to do everything at once - it's hard without any experience. – sashkello Oct 29 '13 at 01:54

3 Answers3

1

This line...

    answerb = input("Your answer: ")

is indented too far. It will only be called in the 'else' portion of the preceding if statement.

answerb = input("Your answer: ")

Unindent it so that it's at the same level as the following if, so that it will always be called (and thus answerb will always be defined).

Amber
  • 507,862
  • 82
  • 626
  • 550
  • But when I unindent it, when I get the correct answer on the first try, it still asks me to input something for answerb. It says "Your answer:" but I already guessed the answer on the first try. – Shahaad Boss Oct 29 '13 at 01:42
  • @ShahaadBoss If you want to repeat the question on a wrong answer, I'd suggest using a loop rather than repeating the code. – Amber Oct 29 '13 at 16:54
0

Edit:

Going by your comment as well as your code structure, I think you want to indent this part:

if answerb == 'Call of duty' or answerb == 'call of duty' or answerb == 'Call Of Duty' or answerb == 'Call of Duty' or answerb == 'a' or answerb == 'a)':
    print("You are correct!")
    points = points + maxGuesses-guessesTaken

Your code should look like this then:

print("Hello and welcome to Shahaad's quiz!") #Introduction
name = input("What is your name? ")
print("Alright", name,", these will be today's topics:")  #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)' or choice == 'a':
    print("You picked Video Games.")
print("Question number one:")                                      #Question 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")       
maxGuesses = 2 #Max number of attempts for the problem
guessesTaken = 0
points = 0
question = input("Your answer: ")
if question == 'Call of duty' or question == 'call of duty' or question == 'Call Of Duty' or question == 'Call of Duty' or question == 'a' or question == 'a)':
    print("You are correct! You guessed the question on the first try!")
    points = points + maxGuesses-guessesTaken
    print("You scored",(maxGuesses-guessesTaken), "points!")
else:
    print("Incorrect!")
    print("You have", (maxGuesses-guessesTaken-1), "guesses remaining!")
    answerb = input("Your answer: ")
    if answerb == 'Call of duty' or answerb == 'call of duty' or answerb == 'Call Of Duty' or answerb == 'Call of Duty' or answerb == 'a' or answerb == 'a)':
        print("You are correct!")
        points = points + maxGuesses-guessesTaken
    print("You scored", (maxGuesses-guessesTaken-1), "points!")
  • But when I unindent it, when I get the correct answer on the first try, it still asks me to input something for answerb. It says "Your answer:" but I already guessed the answer on the first try – Shahaad Boss Oct 29 '13 at 01:43
  • @ShahaadBoss - Wait a minute. I think I know what you want. Is my edit what you are looking for? –  Oct 29 '13 at 01:46
  • I just tried using your edit but I receive an error code for unexpected indent on line 28. – Shahaad Boss Oct 29 '13 at 01:49
  • 1
    @ShahaadBoss - Really? I ran the exact same code I posted and it ran through fine. Make sure it looks _exactly_ like I posted it. Also, do not mix tabs and spaces (if you are). –  Oct 29 '13 at 01:52
  • It worked man, thanks a lot dude! May I ask a question? Why did you put the print on the indent line as line 28? If I wrote this, I wouldve just put print with the same indent as the points = ... – Shahaad Boss Oct 29 '13 at 01:59
  • @ShahaadBoss - Oh, there is no reason. I just did what was needed to be done to fix the script. That line can go in either place. –  Oct 29 '13 at 02:03
-1
  1. No, you haven't defined answerb properly. It is within else statement which may not have been executed. I assume what you want is to indent your last if statement because it is executed only if answer is wrong.

  2. Instead of multiple question == use question in ['bla1', 'bla2', 'bla3']. Here you don't even need this, since you don't care about capitalization. You may do question.lower() == 'bla bla bla' which will match all possible capitalizations. (same for other if's)

  3. The structure of your program is horrible. You need to rewrite it using loops and store your questions and answers in array.

sashkello
  • 17,306
  • 24
  • 81
  • 109
  • If you only read my description, I am a beginner at this, and I haven't learnt some of those terms yet nor am I allowed to use any terms I haven't learnt. Theres no need to be mean about it haha – Shahaad Boss Oct 29 '13 at 01:56
  • @ShahaadBoss 1. I didn't mean to be mean, I'm just trying to emphatically push you into doing things right way from the start, because later it will be harder and harder. 2. There's absolutely nothing non-standard about the things I wrote. If you don't know how they work, google it. – sashkello Oct 29 '13 at 02:05
  • It's not about me not knowing how they work, its about me not being allowed to use things we haven't learnt in class. – Shahaad Boss Oct 29 '13 at 02:15