0

I'm working through some practice questions in the book "Automate the Boring Stuff," and I'm struggling to understand why the code below only produces 'Greetings!' as output.

print('What does spam equal? Hint: Try a number between 1 and 3')
spam = input()
if spam == 1:
    print('Hello')
elif spam == 2:
    print('Howdy')
else:
    print('Greetings!')
CRP31
  • 25
  • 4

2 Answers2

0

This is because the input() returns a string, which is never equal to an integer. Try

spam = int(input())

instead. This will of course throw a ValueError if the input is not an integer.

Dr. V
  • 1,747
  • 1
  • 11
  • 14
0

It does because what you have given as input is stored as string in spam . And when you are using if else statements then it is comparing with integers, but your actual value is a string, therefore it always turns out to be in the final else statement printing Greetings!

So use spam=int(input()) instead.

dewDevil
  • 381
  • 1
  • 3
  • 12