'Why do I keep getting told my variable is outside the function? Python
This is a part of something larger, but my problem is that I keep getting told by the shell that '"return" out of function'. I've tried removing indents and moving it around but it wont go away. Code:
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()
Solution 1:[1]
It's because the return is placed outside a function. The return needs to be placed INSIDE a function. Return is usually placed at the end of a function, see below.
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
Generally it seems like your code is in need of major refactoring. Consider what use the return would have in your code, is it at all necessary?. I also suggest breaking your code down into functions, this makes it generally easier to handle.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
