Home > Enterprise >  What is wrong with this tiny python program?
What is wrong with this tiny python program?

Time:10-15

I'm completely new to python and have been trying to experiment with things that I've been learning as I learn them. One of these things is using if statements. As you can see, when you run the program and input the correct answer which is 11 you will get a "yes!" message. Or else you will get a message that tells you the number you input plus 33 is not 44. However, when I input the correct answer (11) it still tells me that 11 33 is not 44. Curious why this is & if I am missing something?

num_in = input("what   33 is 44?: ")
set_num = str(22   11)

if(num_in   set_num == str(44)):
    print(" Yes!")
else:
    print(num_in   "   "   set_num   " is not 44.")

CodePudding user response:

operator has different meanings for different types. For str it is concatenation, for int add operation in math:

>>> 11   22
33
>>> "11"   "22"
'1122'

In your particular case try to use int everywhere where integer type variable is needed, and format output if necessary:

num_in = int(input("what   33 is 44?: "))
set_num = 22   11

if num_in   set_num == 44:
    print("Yes!")
else:
    print("{}   {} is not 44.".format(num_in, set_num))

CodePudding user response:

Here, num_in and set_num are strings, and string concatenation doesn't follow math. Here, let num_in is "11" and set_num is "33" then set_num num_in is "1133" which is not str(44), just take int values Edited code:

num_in = int(input("what   33 is 44?: "))
set_num = 33

if(num_in   set_num == 44):
    print(" Yes!")
else:
    print(f"{num_in}   {set_num}  is not 44.")
  • Related