2017-07-20 14 views
0

私は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

グローバル宣言を使用すると、[MCVE]を提供してくださいすることができ、これはやや汚いコードですあなたの関数 – DavidG

+0

内である必要がありますか? –

+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

関連する問題