r/pythonhelp • u/jamesfarted09 • Dec 03 '20
Python Syntax Errors
Hi!
I was practicing some pretty easy scripts and all it does is see what a number is and print 'big' or 'small'. so can anyone tell me what the problem is?
x = 5
if x = 5 :
print('big')
if x = 1 :
print('small')
print('done!')
Also im using Pyscripter as an IDE
1
u/scoopulanang Dec 04 '20
It could be indentation but if statements require a double equal sign instead of a single one. For instance,
if x == 5:
#do somethingDouble equal signs is a comparison operator. It compares both values on either side of the operator to see if they are equal. The single equal sign assigns a value to something.
x = 5We assigned a value of 5 to x.
1
u/MT1961 Dec 04 '20
Let's look at what you meant, then see what went wrong:
x = 5
if x == 5 : print('big')
if x == 1 : print('small')
print('done!')
Note, first, that as others have pointed out, you need to indent all Python blocks. Alternatively, you can put it on one line if it is a single statement, as I've done above. Second, as someone else pointed out, you need == for comparing numbers, while = is an assignment. Finally, you can write this a lot more easily:
x = 5
if x == 5 : print('big') elif x == 1 : print('small')
print('done!')
1
u/Tomboyish44 Dec 04 '20 edited Dec 04 '20
Hey, this code might give you the output that you're looking for:
Input:
x = 5
if x == 5:
print('big')
if x == 1:
print('small')
print('done!')Output:
big
done!The difference in assignment operators and equality operators:
I think in the code where you wrote if x = 5 :, I think you intended to check if x is equal to 5, whenever we try to check if something is equal to something else, we use == in place of =. Why? Because we use = to assign values, not to check for equality.
Here = is an assignment operator, and == is an equality operator.
Indentation:
big is supposed to be printed if x is equal to 5, for that it needs to be in the same block of code as the if statement is.
It can either be like this:
if x == 5: print('big')or this:
if x == 5:
print('big')Since the block of code where the print statement has been written is in a new line, we indent it (There's one whitespace, there can be more too). If not indented, you'll get an indentation error.
Also, you can use PyCharm as your text editor if you want to, it's useful to use if you're new to Python, there are other cool editors too, just a suggestion.
1
u/caporalfourrier Dec 03 '20
indentation I assume...