0

enter image description here

I'm practicing using if/elif/else statements, and this program here is supposed to print "Hello" if spam equals 1, "Howdy" if spam equals 2, and "Greetings" if it equals anything else. But for some reason it prints "Greetings" even when spam is equal to 1 or 2.

I even copy and pasted the correct code (from Automate the Boring Stuff with Python Programming) to this practice problem and it still didn't work correctly. Tested it on both PyCharm and the regular IDLE.

print('Please enter a number.')
spam = input()

if spam == 1:
    print('Hello')
elif spam == 2:
    print('Howdy')
else:
    print('Greetings')
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 5
    `spam = int(input())`. – Austin May 05 '19 at 04:32
  • The result of `input()` will always be a string, which means it will never equal the numbers `1` or `2`. Convert to an int as @Austin recommends or test against the strings `"1"` and `"2"`. – Mark May 05 '19 at 04:34

1 Answers1

0

input() returns a string, not an integer.

You can either convert spam to an integer: spam = int(input()) instead of spam = input()

or have the code check for the string version instead:

spam = input()

if spam == '1':
    print('Hello')
elif spam == '2':
    print('Howdy')
else:
    print('Greetings')
sethg
  • 159
  • 1
  • 1
  • 10