+ 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 :)
9 Respostas
+ 2
= is an assignment operator, meaning you are assignment x to that value, thus the if is always true. use == when doing a comparison.
+ 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?
+ 1
Oh your a is not an integer, that's why > was giving you an error.
0
Thanks u for this answer! now i understand. Oh one more question. How do with > or <? like "if x > a:" its same
0
i figured out :) need add int()
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
0
thanks u i figured out ! soon post working guess number game ofc if i done it correctly :)
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.
0
Posted in playgroud guess number game ! my first good code ^^ go check :)