Python Forum

Full Version: Input Question
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
print ('What is your name')
myname= input('Input your name here:')
print ('Nice to meet you, '+myname)
print ('What fact would you like to know? Input a word form of a number 1-20')
fact= input()
print (fact)
if 1==fact:
print('What kind of meat is Spam?', end='Spam is a canned precooked meat product made by the Hormel Foods Corporation, first introduced in 1937. The labeled ingredients in the classic variety of Spam are chopped pork shoulder meat, with ham meat added, salt, water, modified potato starch as a binder, sugar, and sodium nitrite as a preservative.')

In the underlined line, if I input the number '1' while the code is running in the shell, the if statement will not run but an else statement will run. I need help to resolve this problem so the if statement runs if the input is '1'.
It looks you are coding in Python 3.0+. If that's the case, input returns a string. So fact is a string and you are comparing it to an integer, and they will never be equal. You either need to convert face to an integer with int(), or you need to compare it to a string ('1').
(Oct-18-2017, 01:22 AM)ichabod801 Wrote: [ -> ]It looks you are coding in Python 3.0+. If that's the case, input returns a string. So fact is a string and you are comparing it to an integer, and they will never be equal. You either need to convert face to an integer with int(), or you need to compare it to a string ('1').

I've done:
fact= input()
int(fact)
if fact==1:
    print('What kind of meat is Spam?', end='Spam is a canned precooked meat product made by the Hormel Foods Corporation, first introduced in 1937. The labeled ingredients in the classic variety of Spam are chopped pork shoulder meat, with ham meat added, salt, water, modified potato starch as a binder, sugar, and sodium nitrite as a preservative.')
And I still doesn't give me the if statement if I input one
int() doesn't convert the value in place, it returns the converted value. So you need to assign that returned value back to fact (fact = int(fact)). You can also convert it when it comes out of input: fact = int(input()).
(Oct-18-2017, 08:43 PM)ichabod801 Wrote: [ -> ]int() doesn't convert the value in place, it returns the converted value. So you need to assign that returned value back to fact (fact = int(fact)). You can also convert it when it comes out of input: fact = int(input()).

Ahhhh, Thank you, I'm new to Python 3 and pretty rusty.
(Oct-18-2017, 09:15 PM)BlueberryCoconutMuffin Wrote: [ -> ]I'm new to Python 3 and pretty rusty.

Rust also doesn't modify variables in place.