2013-06-28 1 views
-1
import random 
import time 

def set_balance(): 

    print("Welcome to balance manager") 
    print() 
    print("1, Demo mode (10,000 play chips)") 
    print("2, Real mode (PayPal, BTC deposit)") 
    print() 
    choice = int(input("Please enter your selection: ")) 

    if choice == 1: 
     global balance 
     balance = 10000 
     demomode = 1 

    elif choice == 2: 
     global balance 

     balance = int(input("\nEnter the ammount to pay in £")) 

def spin_wheel(): 

    print("\n\n\n\n\n\n\n\nLETS PLAY ROULETTE, YOUR BANK IS, £", balance) 
    print() 
    print("Red, 1") 
    print("Black, 2") 
    print("Please select your colour from the menu below") 

    choice = int(input("\nOption selected ")) 

내가 여기서 잘못 했습니까?UnboundLocalError : 할당 전에 참조 된 'balance'로컬 변수

+1

은 당신이 당신의 코드를 리팩토링 반환 할 수 있습니다 전역 변수, 또는 적어도'global' 키워드에 대한 필요성을 제거하십시오. –

+0

@JustinEthier, 잘 기술적으로 함수는 전역 변수입니다 :) –

+2

@gnibbler - 당신은 실제로나요? - http://tirania.org/blog/archive/2011/Feb-17.html –

답변

2

또한 balance을 메서드 스코프 외부에서 가져 오기 문 아래에 정의해야합니다.

1

일부 코드가 누락되었지만 balance (또는 demomode)을 set_balance에서 전역으로 설정할 필요는 없습니다.

는 다음과 같이 호출이

balance, demomode = set_balance() 

당신은 당신이 원하는 경우 여전히 set_balance의 지역 변수와 같은 이름의 균형을 사용하고, 그냥 내가 제안

def set_balance(): 

    print("Welcome to balance manager") 
    print() 
    print("1, Demo mode (10,000 play chips)") 
    print("2, Real mode (PayPal, BTC deposit)") 
    print() 
    choice = int(input("Please enter your selection: ")) 

    if choice == 1: 
     balance = 10000 
     demomode = True 

    elif choice == 2: 

     balance = int(input("\nEnter the ammount to pay in £")) 
     demomode = False 

    return balance, demomode 
관련 문제