2017-07-20 54 views
0

我遇到shell說local variable referenced before assignment問題,並且感覺以前的答案沒有幫助。我能有一些具體的建議,此代碼:「局部變量在賦值之前引用」的Python問題

import random 

marginalCheck = random.randint(0,1) 

print("This is the political campaign simulator") 

global popularity 

popularity = 50 

def unpopularT(): 
    if popularity <= 30 and popularity < 0: 
     print("You are decreasing in popularity.") 
     popularityRecover = input("Do you wish to carry on or resign. If this carries on, you are likely to lose to a landslide by your Labour opposition") 

     if popularityRecover == "resign": 
      print("You have become an infamous, unpopular politician. You are remembered as a horrible, unkind person") 
     else: 
      print("You are hanging by a thread") 
    elif popularity > 70: 
     print("You are seriously doing well among your supporters and gaining new ones every day") 
    else: 
     print("You are doing fine so far in the campaign") 

def campaignT(): 
      leadershipT = input("You are chosen as a candidate by your colleagues in the Tory leadership contest. Do you wish to take an A - Far Right, B - Right, C - Centre, D - Left or E - Far Left stance on the political spectrum") 

      if leadershipT == "A" or leadershipT == "B": 
       print("You have been elected as leader of the Tories and leader of the opposition. Now the election campaign starts") 

       ClassicToryAusterity = input("What do you wish to do about the poor funding in the NHS by the Labour government. A - Do you wish to keep the current program, B - Do you wish to increase funding dramatically and increase taxes, C - Do you propose minor increases with small raises on tax, D - Do you support austere implementations on the Health Service") 
       if ClassicToryAusterity == "A": 
         popularity += -5 
       elif ClassicToryAusterity == "B": 
         popularity += 5 
       elif ClassicToryAusterity == "C": 
         popularity += 2 
       elif ClassicToryAusterity == "D": 
         popularity += -10 
       BedroomTax = input("What do you propose to do about the bedroom tax. A - increase it, B - freeze it, C - Decrease it, D - Scrap it") 
       if BedroomTax == "A": 
        popularity += -10 
       elif BedroomTax == "B": 
        popularity += -5 
       elif BedroomTax == "C": 
        popularity += -1 
       else: 
        popularity += 10 
       unpopularT() 
      else: 
       print("The Tory whip dislikes your stance and you have not been voted as leader") 

chosenParty = input("Choose a party. A - LibDem, B - Labour, C- Tory") 

if chosenParty == "C": 
    print("You have been elected as a councillor by your fellow peers.") 
    marginality = input("They want you to stand as a MP in a seat. Do you want to stand in a safe or marginal seat") 
if marginality == "marginal": 
    if marginalCheck == 0: 
     print("You have failed to be elected to parliament") 
    else: 
     print("You are duly elected the MP for your constituency!") 
     campaignT() 
else: 
    campaignT() 

我不明白的是,我曾經引用普及作爲一個全局變量,但是根據外殼,它是本地的。

+3

全局聲明必須是你的函數裏面 – DavidG

+0

這有些凌亂的代碼,你可以請提供[MCVE] –

+1

它可能值得一看[爲什麼全局變量是邪惡的?](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil) – DavidG

回答

1

如果你想在函數內部使用全局變量,你不需要聲明任何東西,但是如果你需要用任何方法重新賦值,你需要在函數中聲明它。例如:

test = 'hello' 
def print_test(): 
    print test 

def set_test(): 
    global test 
    test = 'new value' 
你的情況

,你沒有申報全局變量popularity你的函數裏面,你試圖重新分配它。 所以你的情況:

def unpopularT(): 
    global popularity 
    # your code 

def campaignT(): 
    global popularity 
    # your code 
+0

謝謝你解決了它 – Anish

相關問題