我不明白为什么在我的游戏中抛出NameError

时间:2014-05-15 21:31:34

标签: python

代码:

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'")

错误:

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

当我输入&#39; 1&#39;上面的错误出现了。玩游戏&#39;猜数字&#39;它提出了这一点,即使变量difi用于测验。我不确定为什么会这样,所以任何帮助都将不胜感激! 感谢

2 个答案:

答案 0 :(得分:2)

difi只有在quiz为1时才会被绑定:

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

除此之外,您不设置difi,并且在这种情况下未定义名称。

quiz从0开始,除非你选择'2',否则不会递增:

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

另一方面,如果您选择“1”,则quiz保持为0,difi未设置,并且您的代码会中断。

答案 1 :(得分:0)

控制流在定义if difi == ("Easy"):之前到达difi行,因为quiz在顶部仍为0。我怀疑您希望代码的if difi == ("Easy"):和相关部分缩进更多,以便它们出现在if quiz == 1:块中。