So I'm a beginner, and this is for a class I'm taking. I know about return, but it's not letting me do what I'm trying to do in this piece of code
This is the function that contains the variable:
def disp_cookies():
# This stuff is just for a print display
inx = 0 # Displays list vertically
while inx < len(flavor_names):
print("{}. {}".format(inx + 1, flavor_names[inx]))
inx += 1
valid_data = False
while not valid_data:
try:
# This is the variable I need
flavor = int(input("\nSelect a number for flavor> "))
if 0 < flavor <= len(flavor_names):
item_list.append(flavor)
print(flavor_names[int(flavor) - 1])
return flavor # This is the return
break
except Exception:
print("\nError. Please try again")
else:
print("\nPlease enter a valid response")
And here's where I'm trying to use the variable:
print("\n", flavor) # This is outside of the function in the previous snippet btw
print("\n{}s, {} box(es), ${} total".format(flavor_names[flavor - 1], qty_list[order_no], item_total))
This is the error I get:
Traceback (most recent call last):
File "C:\Users\wiche\Documents\School\Python CIS122\L8_orderCost.py", line 95, in <module>
print("\n", flavor)
NameError: name 'flavor' is not defined
I can get rid of the error by defining flavor outside of the function, but then the data in the variable is wrong when I use it. Any idea what I can do to fix it?
Keep in mind I'm an absolute beginner, what you see is basically all I understand of python so far
Thank you!