print(x+=2) won't work in command line

This is the place for queries that don't fit in any of the other categories.

print(x+=2) won't work in command line

Postby crazzyjoel » Tue Aug 13, 2013 10:55 am

Hello, i have an issue and i really want help to solve it. whenever i try to apply this into the command line:

Code: Select all
x = 15
print(x)
print(1001)
print(x+=2)


I get an error message which tells me,
Code: Select all
"File "<stdin>", line 1
print(x+=2)                                      NOTE: There's an arrow which points at the equivalent char :)
SyntaxError: invalid syntax


I'm really new to this and i don't know what to do, haha :). My teacher told me to just use print(x+2) (beacuase this also works) instead of print(x+=2). But i don't think that print(x+2) is the same as print(x+=2). If i got this right, if you only use print(x+2), you give x a temporary new value but if you use print(x+=2) you give x a new value everytime you use that function. In this case, first time you use it, x will turn out to be 17. And next time i use the same function x will become 19 etc...

Perhaps this is a bug? My teacher also told me i could do it something like this and it will also work:

Code: Select all
x = 15
print(x)
x+=2
print(x)

But even if this works, i don't want any bugs in python :(

Any kind people who are willing to give it a try and help me? :)

Best regards, Joel

(This is an example from my schoolbook.)
crazzyjoel
 
Posts: 4
Joined: Tue Aug 13, 2013 10:03 am

Re: print(x+=2) won't work in command line

Postby stranac » Tue Aug 13, 2013 12:15 pm

It's not a bug, it's just something you can't do.
Python doesn't allow you to use assignments in other expressions.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1151
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Baidu [Spider], W3C [Linkcheck] and 2 guests