[英]Why do I keep getting told my variable is outside the function? Python
这是更大的东西的一部分,但我的问题是 shell 一直告诉我““返回”功能异常”。 我试过删除缩进并移动它,但它不会 go 消失。 代码:
def worklodge_cost():
if workshop == 1: # what number the user puts in determines what values they get out of this workshop section
price = 1000
amtdays = 3
elif workshop == 2:
price = 800
amtdays = 3
elif workshop == 3:
price = 1600
amtdays = 3
elif workshop == 4:
price = 500
amtdays = 1
# print("This is your registration fee $", price)
if location == 1: # what number the user puts in determines the price of their lodging
lodgeprice = 150
elif location == 2:
lodgeprice = 225
elif location == 3:
lodgeprice = 175
elif location == 4:
lodgeprice = 300
totalodging = amtdays * lodgeprice # taking the amount of days for the workshop and multiplying it by the lodging price of the location
return totalodging
main()
这是因为return是放在function外面的。return需要放在function的INSIDE里面。return通常放在function的末尾,见下。
def lodgeprice(location, amtdays):
if location == 1: #what number the user puts in determines the price of their lodging
lodgeprice = 150
elif location == 2:
lodgeprice = 225
elif location == 3:
lodgeprice = 175
elif location == 4:
lodgeprice = 300
totalodging = amtdays * lodgeprice #taking the amount of days for the workshop and multiplying it by the lodging price of the location
return totalodging
一般来说,您的代码似乎需要进行重大重构。 考虑返回值在您的代码中有什么用处,是否有必要? 我还建议将您的代码分解为函数,这样通常更容易处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.