Syntax error for the most basic Python code [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I have just started learning python and it seems I am getting errors with one line of code
ERROR MESSAGE:
File "hello.py", line 2
print ‘hello world!’
^
SyntaxError: invalid syntax
FULL CODE
# coding: utf-8
print ‘hello world!’
I'm not really sure what could be going wrong in this one simple line

Use single quotes, to be specific ' instead of ’.

Try:
# coding: utf-8
print 'hello world!'
Python accepts either single ' or double " quotes for strings. On special multiline occasions you can use triple quote '''.

Python 3 requires parentheses after print:
print('hello world')

Related

Ampersand in python dont know what is wrong [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I'm getting this error please help
The ampersand (&) character is not allowed. The & operator is reserved for future use; wrap an ampersand in double quotation marks
("&") to pass it as part of a string.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : AmpersandNotAllowed
This is my code
import jwnet
import songList as sl
sl.downloadSongList(jwnet.ask("Name of your songlist "),
jwnet.ask("Output folder "))
I'm using custom libraries.
jwnet.ask is just like input = input("Question Here")
sl.downloadsonglist downloads a file from a txt
Check where your terminal is based.
You most likely have included an extra & in your code execution path.
e.g. &C:/Users/hello/...

Python - invalid character in identifier error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have this code in a file called script.py
import json
myjson=json.loads(open(‘id.json’,’rb’))
[(os.makedirs(os.path.join(*v.split(‘_’))), open(os.path.join(*v.split(‘_’),‘id.txt’),’w’).write(k)) for k,v in myjson.items()]
I also have in the same folder, a file called id.json that looks like this:
{
"0":"text_test",
"1":"text_text",
"2":"test_test",
...
}
But when I try to run py script.py command on the cmd, I get this error :
File "script.py", line 2
myjson=json.loads(open(‘id.json’,’rb’))
^
SyntaxError: invalid character in identifier
I am not very familiar with Python and some guides from the web didn't help me out.
The quote you are using is invalid.
You are using: ‘
You have to use: " or '
Replace all of your ‘ with '

Unexpected end of file error at comment line [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I was working on a problem and came across a syntax error for a print statement. I started commenting out lines and have an EOF error for a comment line. Any clue how to fix this EOF error for comments?
I've tried commenting out the lines, deleting the lines and typing them again in case I had a typo, and looking up similar solutions
Here is what I am currently trying to run:
# np_baseball is available
# Import numpy
import numpy as n
# Create np_height_in from np_baseball
np_height_in = np_baseball[0]
print(n.median(np_height_in))
# Print out the mean of np_height_in
print(n.mean(np_height_in)
#print(n.median(np_height_in))
# Print out the median of np_height_in
Here's the current error:
File "", line 12
# Print out the median of np_height_in
^
SyntaxError: unexpected EOF while parsing
print(n.mean(np_height_in)
is missing a closing ) . It should be:
print(n.mean(np_height_in))

i don't know how to fix this error, python3 syntax error 'def' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to solve this error.
i tried for almost half hours, but I couldn't find the answer..
This is my error
File "sampling_fun.py", line 71
def average(self) :
^
SyntaxError: invalid syntax
and full code
import csv
class fun :
def __init__(self, rowList, num) :
list = []
listLen = len(self.rowlist)-1
for i in range(listLen) :
list.append(self.rowList[i+1][num] # num = Header 1~4
def average(self) :
ave = sum(self.list)/self.lestLen
print("average : %0.2f" %ave)
return ave
testlist = cssRead('Data_2', 1)
test1 = fun(testlist, 1)
test1.average()
When you encounter such error, check the line before it.
You have a non-matching paranthesis, the closing brace for append is missing.
list.append(self.rowList[i+1][num])
def errors happen when python is not expecting a function definition. As #Siong Thye Goh notes, a missing ) is your issue. For the future, the only time python does not expect a function definition is after an incomplete code-block.
That can happen when you forget to close parentheses or brackets, but also when you forget to put a statement after a colon or don't indent correctly.
Rarely, it can happen due to incompatible space characters when copy-pasting from external sources.

I am not able to run this Python program [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
import random
for i in range(1,21):
print("%10d" %(random.randrange(1,7)),
if (i % 5 == 0):
print ("")
What is wrong in this code ?
I know basic python (almost), but i am not able to figure out what could be error in this program.
it is showing this error:
Syntax Error: invalid syntax at line 6 (if statement)
The fourth line is missing bracket .. Thanks all of you
You missed a right bracket )
print("%10d" %(random.randrange(1,7))),
would be correct
You're missing a ) before the last comma on line 4.
Your parenthesis on line 4 don't match. Because you have an unclosed paren, python doesn't report this syntax error until the colon on line 6 (python ignores line breaks as long as you are inside an enclosing set of parenthesis, brackets, or braces).

Categories