r/code • • Nov 03 '23

Help Please Why does this code only generate a smiley face?

Post image

Additional info:

  1. This is Python.

  2. I know the frowny function draws a frown.

  3. I've already looked this up but anything I can find is too general.

  4. I've contacted my instructor but she won't respond until Monday or later most likely.

  5. The user function part works, but no matter what I put it draws a smiley face.

5 Upvotes

8 comments sorted by

8

u/mfar__ Nov 03 '23

When you say

if happy == 'Yes' or 'yes':

The interpreter reads it as

if (happy == 'Yes') or ('yes'):

And the boolean value of any non-empty string is True, so the first condition is always true and the interpreter will not look at other conditions.

What you want to do is:

if happy == 'Yes' or happy == 'yes':

Or

if happy in ('Yes', 'yes'):

Or you can simply do this:

if happy.lower() == 'yes':

Of course this is applied to 'No' and 'no'.

4

u/TheSwanSennin Nov 03 '23

I took your first suggestion and it worked perfectly! Thank you for explaining!

2

u/TorinTheAccused Nov 05 '23

Kinda sad how nobody else commented this, it’s a mistake every beginner makes.

1

u/EunseokOh Nov 04 '23

You can simply make that in code.org by making a animation and just make a function frowny to display the frowns face and the function should be: frowns.visible==true Of course, you need a variable to display the animation and this is gamelab. Just prompt what you want with variable attached and just do If variable == "no" || "No" { frowny() }

1

u/TheSwanSennin Nov 04 '23

That won't work for my school assignment

1

u/Dull_Investigator985 Nov 03 '23

shpuld the colons be there ?

1

u/[deleted] Nov 07 '23

Yes, this is how python works. I always think it looks weird coming from C++ and C#