Python NameError:未定义全局名称
我的python代码不断得到nameerror,ticketSold上未定义全局变量。我不知道如何解决这个问题,因为我将它定义为一个全局变量。感谢您的帮助Python NameError:未定义全局名称,python,nameerror,Python,Nameerror,我的python代码不断得到nameerror,ticketSold上未定义全局变量。我不知道如何解决这个问题,因为我将它定义为一个全局变量。感谢您的帮助 aLimit=300 bLimit=500 cLimit=100 aPrice=20 bPrice=15 cPrice=10 def Main(): global ticketSold getTickets(aLimit) sectionIncome=calcIncome(ticketSold,aPrice) SectionIncome+
aLimit=300
bLimit=500
cLimit=100
aPrice=20
bPrice=15
cPrice=10
def Main():
global ticketSold
getTickets(aLimit)
sectionIncome=calcIncome(ticketSold,aPrice)
SectionIncome+=totalIncome
print("The theater generated this much money from section A "+str(sectionIncome))
getTickets(bLimit)
sectionIncome=calcIncome(ticketSold,bPrice)
SectionIncome+=totalIncome
print("The theater generated this much money from section B "+str(sectionIncome))
getTickets(cLimit)
sectionIncome=calcIncome(ticketSold,cPrice)
sectionIncome+=totalIncome
print("The theater generated this much money from section C "+str(sectionIncome))
print("The Theater generated "+str(totalIncome)+" total in ticket sales.")
def getTickets(limit):
ticketSold=int(input("How many tickets were sold? "))
if (ticketsValid(ticketSold,limit)==True):
return ticketSold
else:
getTickets(limit)
def ticketsValid(ticketSold,limit):
while (ticketSold>limit or ticketSold<0):
print ("ERROR: There must be tickets less than "+str(limit)+" and more than 0")
return False
return True
aLimit=300
bLimit=500
cLimit=100
aPrice=20
b价格=15
cPrice=10
def Main():
全球售票网
getTickets(aLimit)
部分收入=收入(票证,4月)
部分收入+=总收入
print(“剧院从A区+str(sectionIncome)中产生了这么多钱”)
getTickets(bLimit)
部分收入=收入(票证金额,B价格)
部分收入+=总收入
印刷品(“剧院从B区+str(sectionIncome)产生了这么多钱”)
getTickets(cLimit)
部分收入=收入(票证持有人、cPrice)
部分收入+=总收入
印刷(“剧院从C区+str(sectionIncome)产生了这么多钱”)
打印(“剧院生成”+str(总收入)+“门票销售总额”)
def getTickets(限制):
ticketSold=int(输入(“售出多少张票?”)
如果(ticketsValid(ticketSold,limit)=True):
退票票
其他:
getTickets(限制)
def TICKTSVALID(TICKTSOLD,限制):
虽然(ticketSold>limit或ticketSold说global varname
不会神奇地为您创建varname
。您必须在全局名称空间中声明ticketSold
,例如在cPrice=10
之后global
只确保当您说ticketSold
时,您使用的是全局变量n。)amedticketSold
而不是同名的局部变量。以下是一个版本:
- Python 2/3兼容吗
- 不使用任何全局变量
- 很容易扩展到任意数量的节
- 演示了OOP的一些好处(相对于命名变量的激增:
aLimit
,bLimit
,等等-当您到达27个部分时,您将做什么?)
因此:
import sys
if sys.hexversion < 0x3000000:
# Python 2.x
inp = raw_input
else:
# Python 3.x
inp = input
def get_int(prompt):
while True:
try:
return int(inp(prompt))
except ValueError: # could not convert to int
pass
class Section:
def __init__(self, name, seats, price, sold):
self.name = name
self.seats = seats
self.price = price
self.sold = sold
def empty_seats(self):
return self.seats - self.sold
def gross_income(self):
return self.sold * self.price
def sell(self, seats):
if 0 <= seats <= self.empty_seats():
self.sold += seats
else:
raise ValueError("Cannot sell {} seats, only {} are available".format(seats, self.empty_seats))
def main():
# create the available sections
sections = [
Section("Loge", 300, 20., 0),
Section("Floor", 500, 15., 0),
Section("Wings", 100, 10., 0)
]
# get section seat sales
for section in sections:
prompt = "\nHow many seats were sold in the {} Section? ".format(section.name)
while True:
# prompt repeatedly until a valid number of seats is sold
try:
section.sell(get_int(prompt))
break
except ValueError as v:
print(v)
# report section earnings
print("The theatre earned ${:0.2f} from the {} Section".format(section.gross_income(), section.name))
# report total earnings
total_earnings = sum(section.gross_income() for section in sections)
print("\nTotal income was ${:0.2f} on ticket sales.".format(total_earnings))
if __name__=="__main__":
main()
请修正缩进。请修正缩进。在python中,缩进不是eyecandy,而是语法的一部分。请修正缩进。我没有看到函数calincome
。似乎以前没有人说过。所以请修正缩进。
How many seats were sold in the Loge Section? 300
The theatre earned $6000.00 from the Loge Section
How many seats were sold in the Floor Section? 300
The theatre earned $4500.00 from the Floor Section
How many seats were sold in the Wings Section? 100
The theatre earned $1000.00 from the Wings Section
Total income was $11500.00 on ticket sales.