-2

I am writing a basic program in python to guess a favorite food. I made a variable answer with two options of "pizza" or "Pizza" because they're going to be input from a user and I don't want capitalized words to give a false negative when guessing. However when i test the program and type the 2nd option "Pizza" it does result in an incorrect guess. The very thing i was trying to prevent. I cant figure out why.

answer = "pizza" or "Pizza"
guess = input("What's my favorite food? ")
counter = 1

while (guess != answer):
    counter += 1
    if guess == answer:
        print("Yep! So amazing!")
    else:
        print("Yuck! That’s not it!")
        guess = input("Try again. ")


print("Correct!")
print("Thanks for playing! It took you " + str(counter) + " guesses.")

side question: I think my while loop is written incorrectly for a correct guess... while it works as intended if i type "pizza" this piece of code here :

if guess == answer:
        print("Yep! So amazing!")

doesnt seem to do anything. but its also not messing anything up.

petezurich
  • 9,280
  • 9
  • 43
  • 57
Jon_O
  • 1
  • 2
  • [`or`](https://docs.python.org/3/reference/expressions.html#boolean-operations) doesn't do what you think it does – Sayse Jan 17 '20 at 20:57
  • Python does not support quantum-state variables. You need to learn to detect any of a list of values; see the duplicate for details. Your code sets `answer = True`; the rest of your difficulties follow directly from that error. – Prune Jan 17 '20 at 21:01

1 Answers1

0

answer = "pizza" or "Pizza" evaluates the expression to "pizza" before assigning that value to answer. You want a set that stores both "pizza" and "Pizza", then check if guess is in that set later.

answer = set(["pizza","Pizza"])
guess = input("What's my favorite food? ")
counter = 1

while True:
    counter += 1
    if guess in answer:
        print("Yep! So amazing!")
        break

    print("Yuck! That’s not it!")
    guess = input("Try again. ")

If answer is just a collection of the same string using different upper and lowercase letters, though, just store "pizza" and convert the guess to lowercase before a directly comparison.

answer = "pizza"
...
while True:
    ...
    if guess.lower() == answer:
        ...
chepner
  • 497,756
  • 71
  • 530
  • 681
  • Thank you. Other pages i checked for this answer never showed me `set`. Also `break` is new to me too... previously my code was NOT printing `"Yep. So amazing!"` is that what did it? – Jon_O Jan 17 '20 at 21:14
  • Before, if you guess `pizza` on the first try, `guess != answer` was false, so the loop was never entered. – chepner Jan 17 '20 at 21:33