This question already has an answer here:
Invalid syntax error in python defining a function with PyScripter
(1 answer)
Closed 2 years ago.
I'm on Windows and I'm using PyScripter. For some reason everything results in a syntax error, even code that very obviously does not have a syntax error. For example,
print 6
gets a syntax error, as does,
a = 6
print a
list = (1, 2, 7, 3)
print list
print 3 + 3
or any other code I can think of that involves printing. Did I download Python wrong, am I setting it up wrong, or what?
Are you using Python 3? The print function in Python three must use parentheses:
a = 6
print(a)
I think you installed python 3.x on your machine but you are writing python 2.x syntax in it. Try to install python 2.x or use python 3.x syntax while writing.
**You haven't placed brackets in some parts of your code. Which is why Python is screaming at you. Change
a = 6
print a
list = 1, 2, 7, 3
print list
to
a = 6
print(a)
list = 1, 2, 7, 3
print(list)
Another thing if you're using Python3-x, Python is gonna scream at you. But on Python2-x you needn't put the brackets. From what I understood from the question, you're using Python-3X. Which is why its sending you a syntax error. And I'm pretty sure its nothing because of the Python installation. **
Related
This question already has answers here:
Printing an int list in a single line python3
(11 answers)
Closed 3 years ago.
I am using Python version 3.7.4 on Windows 10 Enterprise.
I am facing a weird issue with Python's print function and especially sep parameter.
In Python REPL, when I use code print(1, 2, 3, 4, 5, sep='\t'), I get proper output as 1 2 3 4 5
However when code tries to iterate over a collection as shown below, instead of showing number separated by a tab, it always displays individual value on a new line.
numbers = {1, 2, 3, 4, 5}
for n in numbers:
print(n, sep='\t')
Can someone please help me to understand why its displaying number value on a separate line?
I have attached screenshot for the reference.
Thanks.
It is because you are iterating over the set numbers. The loop runs and the print() function automatically inserts a \n at the end of each item it prints out for display. Hence each item is being displayed on its own line.
If you wanted to loop through an iterable like in your example and have it separated by a tab, then you can do the following:
for n in numbers:
print(n, end='\t')
By default, the end argument is set to \n.
This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 4 years ago.
coord = {'latitude' : '37.24N' , 'longitude' : '-115.81'}
print 'coordinates: {latitude}, {longitude}' .format(**coord)
I've been recently following this new program on learning python and I've come across an error that says SyntaxError: invalid syntax,but I can't spot any syntax errors, if anyone can offer help it would be much appreciated.
I'm guessing that you are getting a syntax error related to the print statement if you are using the wrong version of Python. Make sure you are using Python 2.x, otherwise upgrade your code to be compatible with Python 3.x.
python --version should tell you which version you are using
Python 3.x version of your code. Notice the round brackets used with the print statement.
coord = {'latitude' : '37.24N' , 'longitude' : '-115.81'}
print('coordinates: {latitude}, {longitude}'.format(**coord))
This question already has an answer here:
Preincrement operators in python
(1 answer)
Closed 5 years ago.
I am a beginner in python and I am using Python 3.5. The python console complains invalid syntax for the below statement:
a = 5
print(a++)
But print(++a) works fine. Can anyone help me understand the difference?
Btw, it seems that print(a+=1) also doesn't work.
Thanks!
++a is just the same as doing (+(+a)). I.E: You're using the mathematical addition operator on the variable a (with implied zeroes). So the result is a
a++ is not valid python syntax (unlike other languages).
a += 1 is an assignment. It is equivalent to a = a + 1 - you can not print an assignment
This question already has answers here:
What does a semicolon do?
(5 answers)
Closed 5 years ago.
I am new to python and currently working in it.
I have write one simple program, as i read somewhere that semicolon not allowed or required in python to end statement. but i have use that and till its working fine! anyone explain me
why its possible?
here is code.
a = 10;
if a == 10:
print "value of a is %s"%(a);
else:
print "value of a is not %s"%(a);
semicolon is allowed as statement separator
>>> a=1;b=2;c=3
>>> print(a,b,c)
1 2 3
It is not required if you write each statement in a new line
Python does not require semi-colons to terminate statements. Semi-colons can be used to delimit statements if you wish to put multiple statements on the same line.
This question already has answers here:
What does "SyntaxError: Missing parentheses in call to 'print'" mean in Python?
(11 answers)
Closed 5 years ago.
I wrote this code, which I found in the book 'Python for dummies'
countdown = 10
while countdown:
print countdown,
countdown -= 1
print "Blastoff!"
It should print 10 9 8 7 6 5 4 3 2 1 Blastoff!
When I run the program, I get an error 'Missing parentheses in call to print'
On youtube I found a code similar to this, which counts from 0 to 10000000. This one works just fine.
def count(x):
while (x <= 10000000):
print (x)
x+=1
count(0)
print ("I hate my job. I quit!")
How comes they look so different? What kind of basic knowledge do I need to understand this? Is this a question of different python versions? Is 'Python for dummies' a bad book?
This is a Python 2 vs 3 gotcha. print was a keyword in 2, now it's a function in 3. Evidently that code was written for 2, but you're using 3.
Just add parentheses and treat print as you would any other function.
For Python 3.0 and after () are required for `print̀€ .
Change to print ("Blastoff!") you won't have any error
The book you are reading is written for python version 2, where if and while statements do not require parenthesis. However, you seem to be using python version 3, which requires you to put paranthesis in if and while statements. You can solve this issue by adding paranthesis to the code in the right places, or download python version 2 (2.7 is the most common version) so you can use the code without modifying it.