Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable not defined
#1
This is very simple (as I am new to coding) but why does this keep saying 'yes' is not defined?
def shop(answer):

    if answer == yes
        return 10
    else:
        return 30
collingwood_shop = yes
grey_shop = yes
print collingwood_shop, grey_shop
Thanks!
Reply
#2
Hello,
It would fit this forum better if you used code tags for the part of your post that contains code. And to change your thread title into something more meaningful. You can edit your post.

if answer == yes
compares variable answer and variable yes. Your variable yes does not appear before, so Python doesn't recognize it.
However, I believe your intention was to use yes as a string. In that case you should put it in quotes ('yes' or "yes").
Reply
#3
On top of the previous comment, I wish to point out that (1) you have a syntax error, a missing colon for your if, and (2) you never call the function you define.

Is this homework? What is your goal? Where are you learning Python from? The code you've posted suggests you might be developing a bad habit of writing too much code before testing what you've written so far.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Variable not defined even though it is CoderMerv 2 58 4 hours ago
Last Post: CoderMerv
  Variable is not defined error when trying to use my custom function code fnafgamer239 4 511 Nov-23-2023, 02:53 PM
Last Post: rob101
  Printing the variable from defined function jws 7 1,160 Sep-03-2023, 03:22 PM
Last Post: deanhystad
  [variable] is not defined error arises despite variable being defined TheTypicalDoge 4 2,041 Apr-05-2022, 04:55 AM
Last Post: deanhystad
  Function will not return variable that I think is defined Oldman45 6 3,434 Aug-18-2020, 08:50 PM
Last Post: deanhystad
  How to assign a module to a variable even if it's not defined? mandaxyz 5 3,160 Aug-12-2020, 10:34 PM
Last Post: snippsat
  Variable not defined Heyjoe 4 2,487 Jul-10-2020, 11:27 PM
Last Post: Heyjoe
  python library not defined in user defined function johnEmScott 2 3,767 May-30-2020, 04:14 AM
Last Post: DT2000
  Error: variable can not be defined julio2000 2 3,137 Feb-09-2020, 08:51 PM
Last Post: julio2000
  Variable defined but python wont recognize it. FWendeburg 3 3,254 Feb-19-2019, 10:43 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020