2017-08-02 72 views
-3

我有下面的代碼有問題:在引用錯誤出現之前調用的Python函數?

#1/usr/bin/env python3 
import random 
financialPoints = 0 
debatePointsT = 0 
marginalCheck = random.randint(0,1) 
debatePointsTO = 0 
Choice = random.randint(0,2) 

popularity = 0 
name = input("What is your first name") 
nameSur = input("What is your surname") 

print("This is the political campaign simulator") 
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() 





def debateT(): 

    #My Code 
    if marginality == "safe": 
      campaignT() 


def campaign(): 
    #My Code 
    if elected == True: 
      debateT() 

它告訴我,我引用它之前被調用的函數,但是我需要它在那裏等我來運行代碼的另一部分,如您可以看到上面。在python中有沒有辦法讓函數並行執行,或者類似的東西?

+0

你是什麼意思,「我需要它在那裏」?是否向你提供了大部分代碼,並且只允許/預期填寫函數體? –

+0

@Anish,你正在底層定義一個函數'campaign',但是在if-else塊中調用'campaignT'函數。除此之外,將函數放在文件的頭部。 –

+0

我需要它,所以我可以使用這些變量 – Anish

回答

0

函數定義必須出現在引用它的代碼之前。

debateT()和移動到文件頂部。

0

移動你定義的函數來你的文件中像的頂部:

#1/usr/bin/env python3 
import random 
financialPoints = 0 
debatePointsT = 0 
marginalCheck = random.randint(0,1) 
debatePointsTO = 0 
Choice = random.randint(0,2) 

def debateT(): 

#My Code 
    if marginality == "safe": 
     campaignT() 


def campaign(): 
    #My Code 
    if elected == True: 
     debateT() 

popularity = 0 
name = input("What is your first name") 
nameSur = input("What is your surname") 

print("This is the political campaign simulator") 
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() 
相關問題