+ 1

Why this is invalid syntax, can someone explame?

x = 1 a = input("Your number") if x = a: print("good") else: print("bad") invalid syntax is "if x = a:" Why? im trying a mini guess number game but its not this code there need import random and i know how all need do but when i put input in "if" it says invalid syntax and dont know way how to fix it or i do something bad? Ty i wait for answer who can explame me :)

17th Nov 2016, 2:10 PM
Lukas Bolevičius
Lukas Bolevičius - avatar
9 ответов
+ 2
= is an assignment operator, meaning you are assignment x to that value, thus the if is always true. use == when doing a comparison.
17th Nov 2016, 2:14 PM
scott johnson
scott johnson - avatar
+ 1
that is saying if x is greater than a. Sorry if I don't understand your question correctly. Is > also giving you invalid syntax?
17th Nov 2016, 2:26 PM
scott johnson
scott johnson - avatar
+ 1
Oh your a is not an integer, that's why > was giving you an error.
17th Nov 2016, 2:30 PM
scott johnson
scott johnson - avatar
0
Thanks u for this answer! now i understand. Oh one more question. How do with > or <? like "if x > a:" its same
17th Nov 2016, 2:23 PM
Lukas Bolevičius
Lukas Bolevičius - avatar
0
i figured out :) need add int()
17th Nov 2016, 2:27 PM
Lukas Bolevičius
Lukas Bolevičius - avatar
0
no no hm. mine english bad.. i want do guess number game. i import random ex import random a = input("number") b = random.randint(1, 10) when i write if a > b: print("number too high") and in if a > b: gives error dont know why
17th Nov 2016, 2:30 PM
Lukas Bolevičius
Lukas Bolevičius - avatar
0
thanks u i figured out ! soon post working guess number game ofc if i done it correctly :)
17th Nov 2016, 2:37 PM
Lukas Bolevičius
Lukas Bolevičius - avatar
0
when using input(), the data read in is put into a string variable, even if you only give it an integer. I haven't delved too much into python yet, but I'm sure there's a way to convert the string to an integer or the other way around, but type conversions like that are not guaranteed to work flawlessly.
17th Nov 2016, 2:38 PM
scott johnson
scott johnson - avatar
0
Posted in playgroud guess number game ! my first good code ^^ go check :)
17th Nov 2016, 2:58 PM
Lukas Bolevičius
Lukas Bolevičius - avatar