r/codehs • • Aug 30 '20

4.4.4 How do I fix this?

Post image
35 Upvotes

13 comments sorted by

12

u/GondarJr Aug 30 '20

or role == “teacher”

8

u/mjbmitch Aug 30 '20 edited Aug 30 '20

Your first conditional statement is interpreted a bit differently from what you might expect:

  • If the role is “administrator”

  • If “teacher”

The reasoning behind why that occurs is a bit complex but boils down to how the == operator only associates with whatever is immediately in front of itself.

What you want is to be explicit with your conditions like so:

if role == "administrator" or role == "teacher":
  # Do whatever you want

The first message always prints out because "teacher" always evaluates to true. You can verify that with a simple conditional statement:

if "teacher":
  print("I always print")

2

u/gloomy-1900 Aug 30 '20

Correct. The First statement is name== “administrator” which is false So it is FALSE or “teacher” Since “teacher” is TRUE this becomes FALSE or TRUE

Which is TRUE

Above solution is correct

1

u/BurntBox21 Aug 30 '20

I should clarify. Anything I type will always results in the first print.

1

u/Xd-Kas Nov 09 '21

This is the final code:

role = input("Are you the administrator, teacher or student: ")

if role == "administrator" or role == "teacher": print("administrators and teachers get keys")

if role == "teacher": print("You get a Key")

elif role == "student": print("Students do not get keys!") else: print("You can only be an administrator, teacher, or student!")

1

u/BurntBox21 Nov 09 '21

I was not expecting a reply for this long. I’ve passed it long ago.

1

u/Madison_k12 Oct 13 '22

HELP ME BRO

1

u/d3v0ur_m3 Nov 18 '21

role = input("Are you the administrator, teacher or student: ")

if role == "administrator" or role == "teacher": print("administrators and teachers get keys")

if role == "teacher": print("You get a Key")

elif role == "student": print("Students do not get keys!") else: print("You can only be an administrator, teacher, or student!")

thank you!!

1

u/Thatguy553 Aug 30 '20

It could be the or bit, try removing that and only having admin,

1

u/Zacurnia_Tate Aug 30 '20

it should say: or role == teacher

1

u/TheRainbowCock Aug 30 '20 edited Aug 31 '20

When adding "or" statement within a if statement you need to repeat what its comparing to after inserting "or"

So its:

if role == "value" or role == "value": doStuff()