2014-11-07 2 views
-2
import random 
import time 

global score 
global cpuscore 

score = 1 
cpuscore = 1  


print("Current score CPU:Player = 1 , 1 ") 

def plus(): 

    rand1 = random.randint(2,99) 

    rand2 = random.randint(2,99) 

    ans = (rand1 + rand2) 

    print("What's", rand1, "+" ,rand2,) 

    quest = int(input())      

    if quest == ans: 

     print("Correct") 

     score + 1 

    else: 

     print("Incorrect") 

     cpuscore + 1 

def multiply(): 

    rand1 = random.randint(2,99) 

    rand2 = random.randint(2,99) 

    ans = (rand1 + rand2) 

    print("What's", rand1, "+" ,rand2,) 

    quest = int(input())      

    if quest == ans: 

     print("Correct") 

     score + 1 

    else: 

     print("Incorrect") 

     cpuscore + 1 


def subtract(): 

    rand1 = random.randint(2,99) 

    rand2 = random.randint(2,99) 

    ans = (rand1 + rand2) 

    print("What's", rand1, "+" ,rand2,) 

    quest = int(input())      

    if quest == ans: 

     print("Correct") 

     score + 1 

    else: 

     print("Incorrect") 

     cpuscore + 1 


print("Welcome to eleven elves") 

print("Please name your elves") 


elf1 = input("Elf 1: ") 
elf2 = input("Elf 2: ") 
elf3= input("Elf 3: ") 
elf4 = input("Elf 4: ") 
elf5 = input("Elf 5: ") 
elf6 = input("Elf 6: ") 
elf7 = input("Elf 7: ") 
elf8 = input("Elf 8: ") 
elf9 = input("Elf 9: ") 
elf10 = input("Elf 10: ") 
elf11 = input("Elf 11: ") 



print("So let me just clarrify, the name of your eleven elves throughout this quest will be") 




clar = str.lower(input("Is this correct? (Yes or No) ")) 

if clar == ("yes"): 
    print("Ok, let's continue on") 
else: 
    print("Would you to change the name of your elves?") 
    clar2 = str.lower(input("Yes or No ")) 
    if clar2 == ("yes"): 
     elf1 = input("Elf 1: ") 
     elf2 = input("Elf 2: ") 
     elf3= input("Elf 3: ") 
     elf4 = input("Elf 4: ") 
     elf5 = input("Elf 5: ") 
     elf6 = input("Elf 6: ") 
     elf7 = input("Elf 7: ") 
     elf8 = input("Elf 8: ") 
     elf9 = input("Elf 9: ") 
     elf10 = input("Elf 10: ") 
     elf11 = input("Elf 11: ") 
     print("Elf names changed.") 
    else: 
     print("Ok no problem, let's continue on") 

print(elf1, "enters a cave... The cave is full of monsters and creepy crawlys, a monster pops out    of no where, the monster says.. Dear old dear old", elf1 ,"Why are you all by your self? You wanna get past, you gotta' get asked do you accept or run away?") 
q1 = str.lower(input("Do you accept the challenge... ('you get the drift now yes or no....') ")) 

if q1 == ("yes"): 

    plus() 

    print("Current score CPU:Player", cpuscore, score) 

else: 

    print(elf1, "runs away") 

    print(cpuscore) 

"cpuscore"에 1을 더하는 것과 같은 방식으로 "score"에 1을 더하고 싶습니다. 코드를 수정하고 올바른 질문을 얻으면 여전히 새로운 점수를 1로 인쇄하므로 혼란스럽고 그 이유를 알 수 없습니다. 나는 파이썬에 익숙하지 않아서 정말 도움이되지 않을 것이다.누군가 내 코드 (파이썬 함수)를 도와 줄 수 있습니까?

문제는 여전히

Traceback (most recent call last): 
    File "N:\Open Me x\Computing\Mrs Farakh\Programming\Python\Assessment.py", line 139, in <module> 
plus() 
    File "N:\Open Me x\Computing\Mrs Farakh\Programming\Python\Assessment.py", line 27, in plus 
    score += 1 
UnboundLocalError: local variable 'score' referenced before assignment 
+0

코드 스 니펫이 추적 코드와 일치하지 않습니다. 게시물을 수정하여 문제를 명확히하십시오. –

+0

나는 Bhat Irshad의 대답을 구현하려고했을 때 일어났던 일을 "이 문제는 여전히 지속된다"는 진술과 추적보고가 설명한다고 생각한다. 대답이 잘못 되었기 때문에 의미가 있습니다. –

답변

-1

정수는 파이썬에서 immutable 있습니다 지속. 내부에파이썬의 정수로 수정하면 C과 같습니다. 모든 증분 값을 다시 할당해야합니다.

교체 :

score + 1 

score += 1 # or score = score + 1 

cpuscore + 1 # or cpuscore = cpuscore + 1 

에 의해에 의해

cpuscore += 1 

score + 1 

그것은해야한다,

score = score + 1 

당신이 필요

def plus(): 
    global score 
    global cpuscore 

def subtract(): 
    global score 
    global cpuscore 
+3

'global' 키워드는 함수 몸체 밖에서는 쓸모가 없습니다. –

+0

스크립트 시작 부분이 아닌 plus() 함수 본문에 전역 문을 넣으면 다른 제안 된 변경 사항이 적용됩니다. multiply() 함수는 똑같은 문제가 있지만 결코 호출되지 않습니다. –

0

코드의 문제는 이것이다 : 당신이로 그 변수를 수정하는 모든 함수에서 변수는 글로벌 확인 전역 변수를 할당하는 def 함수를 작성하십시오. 그렇지 않으면 전역 함수가 완전히 쓸모가 없습니다.

희망이 도움이됩니다.

관련 문제