2016-04-12 44 views
0

我是初學者編碼者,我有這個遊戲,但我的問題是,在我的遊戲中,用戶有點可以花在戰鬥中。並且它應根據您花費的多少更新點變量。我的全局變量的答案?

global Points 
    Points = int(100) 
def Combat(): 
    print('how much points would you spend(higher points = higher chance of winning') 
    a9 = int(input('>')) 
    Points = int(Points)- int(a9) 
    global Points 
    print('You have ',Points,' points') 

但由於某些原因,它不會改變全局點變量,任何答案? 這是否有答案? 還是未解決?

+0

給出的答案是絕對正確的,將完全解決您的範圍問題!我會推薦研究Python的面向對象編程,以便充分理解是什麼讓語言變得如此強大!類是避免範圍問題並大大減少重新輸入的好方法!祝你好運,祝好! – TheLazyScripter

回答

3

您需要global Points來分配它的函數內的值之前,否則,您使用的是Points是本地的功能和隱藏你想使用的全球積分:

Points = 100 

def Combat(): 
    print('how much points would you spend(higher points = higher chance of winning') 
    a9 = int(input('>')) 
    global Points 
    Points = Points - int(a9) 
    print('You have ',Points,' points') 

而且,您不需要首次使用global Points,因爲外部函數自動成爲全局範圍。