2014-05-15 2 views
-1

코드 :는 이해가 안

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

오류 : 나는 '1'게임 '을 플레이하기 입력 할 때

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 

오류가 위 온다 변수 difi가 퀴즈에 대한 것이지만 숫자를 추측하십시오. 왜 이런 일이 일어나는지 모르겠으니 어떤 도움을 주시면 감사하겠습니다!

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

당신은 그렇지 difi를 설정하지 않고, 이름이이 경우에 정의되지 않은 : 감사

+2

[this] (http://www.sscce.org/) 및 [this] (http://stackoverflow.com/help/mcve)를 읽고 코드를 문제의 최소한의 예로 줄이십시오. 또한, 덜 끔찍한 제목을 생각해 내십시오. – jonrsharpe

+0

'game == ("1")'및 다른 유사한 행에 왜 괄호가 있습니까? –

+0

이 코드는 잘 구성되어 있지 않습니다. 적어도 각 종류의 게임을 다른 기능 또는 더 나은 자체 클래스로 만들어야합니다. – cmd

답변

2

difiquiz가 1 인 경우에만 적 바인딩됩니다. 당신이 '2'를 선택하지 않는 한

quiz 0에서 시작하고는 증가하지 않습니다 :

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

당신이 '1'을 선택하면, 다른 한편으로는, quiz 0 유지, difi가 설정되지 않은, 코드가 깨집니다.

+0

그래서 어떻게 내가 이것에 대응하겠습니까? – user3642598

+0

그건 당신이 그 사건을 어떻게 처리 하느냐에 달렸습니다. '퀴즈 = 0' **은 무엇을 의미합니까 **? 아마도 기본값으로'difi = 'Easy''를 시작 부분에 설정해야할까요? –

+0

글쎄, 나는 그것을 게임을 차별화하는 방법으로 사용하고 있었다. 이것을하는 것이 더 효율적입니까? – user3642598

0

difi이 정의되기 전에 제어 흐름이 if difi == ("Easy"): 행에 도달했습니다. quiz이 여전히 맨 위에 0이기 때문에 제어 흐름이 정의되어 있습니다. if difi == ("Easy"):과 관련 코드 부분이 더 들여 쓰기되도록 지정 했으므로 if quiz == 1: 블록 안에 나타납니다.

관련 문제