Confused as to why this code doesn’t work sometimes.
So I’m super beginner and working through a course using jupyter notebooks and having some issues with a fishstore assignment asking for inputs for fish name and price.
The code I originally used was
def fishstore(fish, price): fish = input("What kind of fish?").title() price = input("What is the price of the fish") return(fish + " costs $"+ price)
print(fishstore(fish, price))
And returns this error
NameError Traceback (most recent call last) <ipython-input-1-724725085cab> in <module> 4 return(fish + " costs $"+ price) 5 ----> 6 print(fishstore(fish, price))
NameError: name 'fish' is not defined
So I rearranged the code as follows
fish = input("What kind of fish?").title() price = input("What is the price of the fish") def fishstore(fish, price): return(fish + " costs $"+ price)
print(fishstore(fish, price))
Which worked. But now the real confusion begins. When I copy and paste the original code back into the notebook it suddenly works fine but never works in a new notebook. Meanwhile the latter code works fine in a new notebook and the original notebook. Can anyone explain this to me?
Posted Jan 25, 2021, 4:54 PM UTC · edited
The two variables you've defined remain in the state so your first function will now work.
Gotcha thank you