+ 1

Booleans and If statements

i had been trying this : num = 7 if num ==7: print("num" =="7") the output is : False but if i make this : num = 7 if num ==7: print("7" =="7") the output is : True and my question is why?

11th Aug 2017, 10:32 AM
jesús lópez martín
jesús lópez martín - avatar
3 Respuestas
+ 1
Because the first statement compares the string "num", not the value of the variable num.
11th Aug 2017, 10:51 AM
Jonas Schröter
Jonas Schröter - avatar
+ 1
You should remove the second and third quote because you want everything in the print function to be one string. Try print("num == 7") It reads your version as a question. Like "Hey code, tell me if the string 'num' is exactly the same as the string '7'. Then it will print false Another example: if you say print("num" + "7") it will print out "num7"
11th Aug 2017, 10:56 AM
Tim Haerkens
Tim Haerkens - avatar
0
7 is an integer and "7" is an string 7 == "7" :False nums = "7" print (nums == "7") output: True
1st Sep 2017, 7:33 PM
sebastiaan