-2

Neewbie here. I'm doing CS50p and found myself confused about one of first problem sets.

In deep.py, implement a program that prompts the user for the answer to the Great Question of Life, the Universe and Everything, outputting Yes if the user inputs 42 or (case-insensitively) forty-two or forty two. Otherwise output No.

So I made this:

answ = input("What is Answer to the Great Question of Life, the Universe and Everything? ")

if answ == "42" or "forty two" or "forty-two":
    print("Yes")
else:
    print("No")

But output is always "yes".

However if I slice it like that:

answ = input("What is Answer to the Great Question of Life, the Universe and Everything? ")

if answ == "42":
    print("Yes")
elif answ == "forty-two":
    print("Yes")
elif answ == "forty two":
    print("Yes")
else:
    print("No")

It works.

Much thanks for simple answere.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
shardival
  • 97
  • 1
  • that's not how logical operators work. you either want `if answ == "42" or answ == "forty two" or answ == "forty-two":` or even better `if answ in ("42", "forty two", "forty-two"):` – Sembei Norimaki Feb 10 '23 at 14:16

2 Answers2

1

You are so close but you just have to add in the call for answ within the or clause every time

answ = input("What is Answer to the Great Question of Life, the Universe and Everything? ")

if answ == "42" or answ == "forty two" or answ == "forty-two":
    print("Yes")
else:
    print("No")
Hillygoose
  • 177
  • 8
0

The problem with your first implementation is the condition in the if statement. The condition answ == "42" or "forty two" or "forty-two" is always True because the expression "forty two" or "forty-two" is always True, as the string "forty two" is considered a truthy value in python.

sm3sher
  • 2,764
  • 1
  • 11
  • 23