Why do I keep coming up with an error at the end of my code?
Question:
So when inputting my third elif variable in my main function, my other functions don’t work. My question, is what’s stopping it. Lines 123, 124, 125, keep receiving the "UnboundLocalError: totalcost referenced before assignement"
`
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 Cleaningn2-Deep Cleaningn"))
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 Cleaningn2-Deep Cleaningn"))
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 Cleaningn2-Deep Cleaningn"))
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 Serivcen2-Yard Servicen3-Bothn"))
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()
`
The process works when trying to do elif on 1 and 2, however it completely shuts down when attempting to work within the last version process.
Answers:
So I ran your code and I got an error on line 129 and 122 and I think I figured out what the error problem is.
I think you might have an error with not globalizing the variable so try doing:
global totalcost
for each of the variables or something like that. Good luck!
You have a typo in main()
:
elif YHB == 3:
toatlcost = homeservice() + YardService()
You’ve misspelled "totalcost".
That’s A bug at any rate. You also have some other issues that’ll crop up under different cases – it’s possible to call a number of your functions and return None
for totalcost – which will trip you up when you later try to add that return to something. I suggest you ensure a return of something numeric – zero for want of anything better.
So when inputting my third elif variable in my main function, my other functions don’t work. My question, is what’s stopping it. Lines 123, 124, 125, keep receiving the "UnboundLocalError: totalcost referenced before assignement"
`
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 Cleaningn2-Deep Cleaningn"))
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 Cleaningn2-Deep Cleaningn"))
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 Cleaningn2-Deep Cleaningn"))
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 Serivcen2-Yard Servicen3-Bothn"))
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()
`
The process works when trying to do elif on 1 and 2, however it completely shuts down when attempting to work within the last version process.
So I ran your code and I got an error on line 129 and 122 and I think I figured out what the error problem is.
I think you might have an error with not globalizing the variable so try doing:
global totalcost
for each of the variables or something like that. Good luck!
You have a typo in main()
:
elif YHB == 3:
toatlcost = homeservice() + YardService()
You’ve misspelled "totalcost".
That’s A bug at any rate. You also have some other issues that’ll crop up under different cases – it’s possible to call a number of your functions and return None
for totalcost – which will trip you up when you later try to add that return to something. I suggest you ensure a return of something numeric – zero for want of anything better.