Skip to content
Sign upLog in
← Back to Community

Code giving me all script answers?

Profile icon
Mozyyoo

(Sorry about making another one of these problems, i just have no idea why my program is doing this now)
So i have a text-based game and it was doing fine, and now for some reason, if i put a choice, it spits out all of the choices.
For example:
print ("[Shopkeeper] Hello ther- Oh! You're not a ghost...")
print ("\nSay: \nA. Well, you're not a human. \nB. No need to be so rude. \nC. Ah, yeah.")
shopkeeperanswer=input()
if shopkeeperanswer == "A" or "a":
print ("[Shopkeeper] True, true.")
if shopkeeperanswer == "B" or "b":
print ("[Shopkeeper] Oh, sorry... I've just never met one of your kind before...")
if shopkeeperanswer == "C" or "c":
print ("[Shopkeeper] It's ok, i was just so amazed to meet one of your kind! Anyway, welcome to the shop!")

The code when ran will spit out "True, true", "Oh sorry, i've just never met one of your kind before..." and "it's ok, i was just so amazed to meet one of your kind! Anyway, welcome to the shop!".
(I did previously import some stuff to make a title screen so is that why??)

(Also the error only starts after the user is done talking to Kingston)

Answered by 19wintersp [earned 5 cycles]
View Answer
Voters
Profile icon
RayyanEl-Homsi
Profile icon
ch1ck3n
Profile icon
Mozyyoo
Comments
hotnewtop
Profile icon
19wintersp

This issue keeps happening to people. In Python, this:

myvar = 0 if myvar == 1 or 2: print("myvar is 1 or 2")

would print "myvar is 1 or 2". This is because of how the condition is interpreted; not like this:

if (myvar == (1 or 2))

but like this:

if ((myvar == 1) or (2))

In Python, 2 is truthy, and so the if block runs. You should either check whether the variable is in a list of options:

if myvar in [1, 2]: print("myvar is 1 or 2")

or check each option individually:

if myvar == 1 or myvar == 2: print("myvar is 1 or 2")