简体   繁体   中英

I don't understand why a NameError is thrown in my game

Code:

import random
score = 0
score2 = 0
guess = 0     #Defining Variables
quiz = 0
lives = 3
print('''Would you like to play a 'Guess the number' or a '3 round quiz'?
For Guess the number enter '1', for 3 round quiz enter '2' ''')
game = input()
if game == ("1"):
    guess = guess + 1
    print("Ok, time to start")   #Asking what game you want to play
elif game == ("2"):
    quiz = quiz + 1
else:
    print("Please answer with 'Guess the number game' or a '3 round quiz'")
if quiz == 1:
    print("Would you like to play 'Easy' or 'Hard'?")
    difi = input()
if difi == ("Easy"): 
    print("Ok, let's go!")     #Choosing difficulty
    score = score + 1
elif difi == ("Hard"):
    print("Ok,let's go")
    score2 = score2 + 1
else:
    print("Please answer with 'Easy' or 'Hard'")
if score == 1:
    num1 = (random.randint(1, 50))
    num2 = (random.randint(1, 50))
    print("First question")
    print("What is ",num1,"+",num2) # Question 1 easy
    ans1 = input()
    ans1 = int(ans1)
    if ans1 == num1+num2:
        print("Well done")
        score = score + 1
    else:
        print("Unlucky, it was ",num1+num2)
if score == 2:
    num3 = (random.randint(1, 10))
    num4 = (random.randint(1, 10))
    print("Next question")
    print("What is ",num3,"*",num4) # Question 2 easy
    ans2 = input()
    ans2 = int(ans2)
    if ans2 == num3*num4:
        print("Congratualtions, on to the last question")
        score = score + 1
    else:
        print("Unlucky, it was ",num3*num4)
if score == 3:
    num5 = (random.randint(1, 5))
    num6 = (random.randint(1, 2))
    print("What is ",num5,"**(To the power of)",num6) # Question 3 easy
    ans3 = input()
    ans3 = int(ans3)
    if ans3 == num5**num6:
        print("Congratualtions, you beat the game on easy")
        print("Now try hard!")
        score = score + 1
    else:
        print("Unlucky, it was ",num5**num6)
if score == 4:
    print("Would you like to try hard?")
    hard2 = input()
else:
    print("Ok, come back later")      # If you beat easy you can choose to play hard here
if hard2 == ("Yes"):
    print("This is the hard game, good luck!")
    score2 = score2 + 1
elif hard2 == ("No"):
    print("Ok, see you soon")
else:
    print("Please answer with 'Yes' or 'No'")
if score2 == 1:
    num12 = (random.randint(1, 500))
    num22 = (random.randint(1, 500))
    print("First question")
    print("What is ",num12,"+",num22) # Question 1 hard
    ans12 = input()
    ans12 = int(ans12)
    if ans12 == num12+num22:
        print("Well done")
        score2 = score2 + 1
    else:
        print("Unlucky, it was ",num1+num2)         #2s in front of all hard variables so it differentiates the variables                                          #From Easy and Hard
if score2 == 2:                                     
    num32 = (random.randint(1, 25))
    num42 = (random.randint(1, 25))
    print("Next question")
    print("What is ",num32,"*",num42) # Question 2 hard
    ans22 = input()
    ans22 = int(ans22)
    if ans22 == num32*num42:
        print("Congratulations, on to the last question")
        score2 = score2 + 1
    else:
        print("Unlucky, it was ",num32*num42)
if score2 == 3:
    num52 = (random.randint(1, 15))
    num62 = (random.randint(1, 3))
    print("What is ",num52,"**(To the power of)",num62) # Question 3 hard
    ans32 = input()
    ans32 = int(ans32)
    if ans32 == num52**num62:
        print("Congratualtions, you beat the game on hard")
    else:
        print("Unlucky, it was ",num52**num62)
if guess == 1:
    print("Time to play")      #Guess the number game
    guess = guess + 1
if guess == 2:
    print("Pick a number between 1 an 10")
    comp_num = (random.randint(1,10))
    user_guess1 = input()
    user_guess1 = int(user_guess1)
if user_guess1 == comp_num:
    print("Well done, you beat the game on your first turn!")
else:
    print("Unlucky you still have 2 more goes")
    lives = lives - 1
if lives == 2:
    print("Guess again")
    user_guess2 = input()
    user_guess2 = int(user_guess2)
if user_guess2 == comp_num:
    print("Congrats, you beat it on your second guess!")
    lives = lives - 1
if lives == 1:
    print("Guess again")
    user_guess3 = input()
    user_guess3 = int(user_guess3)
if user_guess3 == comp_num:
    print("Congrats, you beat it on your last life!")
    lives = lives - 1
else:
    print("Unlucky, care to try again")
if lives == 0:
    retry = input()
if retry == ("Yes"):
    lives = lives + 3
elif retry == ("No"):
    print("Ok, come back soon")
else:
    print("Please answer with 'Yes' or 'No'")

Error:

Traceback (most recent call last):
  File "C:\Users\Boys\Desktop\3 Round Quiz.py", line 24, in <module>
    if difi == ("Easy"):
NameError: name 'difi' is not defined

The error above comes up when I enter '1' to play the game 'Guess the number' it comes up with that, even though the variable difi is for the quiz. I'm not sure why this happens so any help will be appreciated! Thanks

difi is only ever bound if quiz is 1:

if quiz == 1:
    print("Would you like to play 'Easy' or 'Hard'?")
    difi = input()

You don't set difi otherwise, and the name is not defined in that case.

quiz starts at 0, and isn't incremented unless you pick '2':

elif game == ("2"):
    quiz = quiz + 1

If you pick '1', on the other hand, quiz remains at 0, difi is not set, and your code breaks.

The control flow is reaching the if difi == ("Easy"): line before difi is defined because quiz is still 0 at the top. I suspect you intended the if difi == ("Easy"): and associated parts of the code to be indented more so they appear inside the if quiz == 1: block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM