我遇到以下代码的问题:
#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)
函数定义必须位于引用它的代码之前。
将debateT()
和campaign()
移至文件顶部。
答案 1 :(得分: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()