因此,當在我的主函式中輸入我的第三個 elif 變數時,我的其他函式不起作用。我的問題是什么阻止了它。第 123、124、125 行,不斷收到“UnboundLocalError: totalcost referenced before assignment”
`
def welcome():
print("Hello and Welcome Super Maids")
def LC (a , b):
totalcost = 150 a (b * 10)
print("The total of your service is:\n$",totalcost)
return totalcost
def DC (a, b):
totalcost = 300 a (b * 10)
print("The total of your service is:\n$",totalcost)
return totalcost
def SeniorDiscount(totalcost):
ans = str(input("Are you by a chance a serior citizen? [y/n]"))
if ans == 'y':
age = eval(input("What year were you born? [XXXX]"))
if age >= 1960:
print("Perfect! Thank you, sir/ma'am!")
discount = totalcost * .15
print("Your discount is:\n$", discount)
totalcost = totalcost - discount
print("Great! The total cost of your serivce is:$",totalcost)
return totalcost
elif ans == 'n':
print("Thank you for being honest!")
print("Great! The total cost of your serivce is:$",totalcost)
return totalcost
else:
print("We'll need an actual answer from you in order to continue")
SeniorDiscount(totalcost)
return totalcost
def YardService():
shrubCost = 10
edgingCost = 20
sqftCost = 20
sqrft = eval(input("What is the square footage of your yard?\n"))
linsqft = eval(input("What is the linear square footage of your yard?\n"))
shrubs = eval(input("How many shrubs do you have?\n"))
totalcost = (shrubCost * shrubs) (edgingCost linsqft) (sqftCost * sqrft)
return totalcost
def YardService1():
shrubCost = 10
edgingCost = 20
sqftCost = 20
sqrft = eval(input("What is the square footage of your yard?\n"))
linsqft = eval(input("What is the linear square footage of your yard?\n"))
shrubs = eval(input("How many shrubs do you have?\n"))
totalcost = (shrubCost * shrubs) (edgingCost linsqft) (sqftCost * sqrft)
return totalcost
def homeservice():
welcome()
NumberOfRooms = eval(input("How many rooms are in need of cleaning?\n"))
if NumberOfRooms < 3:
print("Your starting total is: $", 120)
CostOfRooms = 120
windows = eval(input("How many windows do you have in need of cleaning?\n"))
CostOfWindows = 10
TypeClean = eval(input("What kind of cleaning service are you in search of?\n1-Light Cleaning\n2-Deep Cleaning\n"))
if TypeClean == 1:
totalcost = LC(CostOfRooms,windows)
return totalcost
elif TypeClean == 2:
totalcost = DC(CostOfRooms,windows)
return totalcost
else:
print("You must select a service in order to continue")
elif 2 < NumberOfRooms < 4:
print("Your starting total is: $", 150)
CostOfRooms = 150
windows = eval(input("How many windows do you have in need of cleaning?\n"))
CostOfWindows = 10
TypeClean = eval(input("What kind of cleaning service are you in search of?\n1-Light Cleaning\n2-Deep Cleaning\n"))
if TypeClean == 1:
totalcost = LC(CostOfRooms,windows)
return totalcost
elif TypeClean == 2:
totalcost = DC(CostOfRooms,windows)
return totalcost
else:
print("You must select a service in order to continue")
elif 3 < NumberOfRooms < 6:
print("Your starting total is: $", 175)
CostOfRooms = 175
windows = eval(input("How many windows do you have in need of cleaning?\n"))
CostOfWindows = 10
TypeClean = eval(input("What kind of cleaning service are you in search of?\n1-Light Cleaning\n2-Deep Cleaning\n"))
if TypeClean == 1:
LC(CostOfRooms,windows)
return totalcost
elif TypeClean == 2:
DC(CostOfRooms,windows)
return totalcost
else:
print("You must select a service in order to continue")
elif NumberOfRooms >= 6:
print("We do not offer a service of this size")
def main():
print("We'll start by asking you a set of questions")
YHB = eval(input("What kind of cleaning service are you in search of?\n1-Home Serivce\n2-Yard Service\n3-Both\n"))
if YHB == 1:
totalcost = homeservice()
SeniorDiscount(totalcost)
elif YHB == 2:
totalcost = YardService()
SeniorDiscount(totalcost)
elif YHB == 3:
toatlcost = homeservice() YardService()
#print(totalcost)
#YardService1(totalcost)
SeniorDiscount(totalcost)
else:
print("You must have selected nothing, we'll try again.")
main()
#--- Execute --------------------------------------------------------
main()
`
該程序在嘗試在 1 和 2 上執行 elif 時有效,但是在嘗試在最后一個版本行程中作業時它完全關閉。
uj5u.com熱心網友回復:
你有一個錯字main():
elif YHB == 3:
toatlcost = homeservice() YardService()
您拼錯了“總成本”。
無論如何,這是一個錯誤。您還會遇到一些其他問題,這些問題會在不同情況下出現 - 可以呼叫許多函式并回傳總None成本 - 當您稍后嘗試將該回傳添加到某些東西時,這會讓您感到困惑。我建議您確保回傳一些數字 - 零表示需要更好的東西。
uj5u.com熱心網友回復:
所以我運行了你的代碼,在第 129 行和第 122 行出現錯誤,我想我知道錯誤問題是什么。
我認為您可能會因未全球化變數而出錯,因此請嘗試執行以下操作:
global totalcost
對于每個變數或類似的東西。祝你好運!
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/528267.html
標籤:Python功能返回
