Why is the print() not working!

Starting with Python, trying to do a sum inside a for loop and then print the total Outside of the for loop. Have the right indentation in place to do this but still print() returns a Syntax error! Please suggest where is this going wrong.

File “”, line 3
print(total)
^
SyntaxError: invalid syntax

```py3

YOUR CODE GOES HERE

total=0
for line in open(’/home/values’):
total=total + int(line.replace(’,’,’’))
print(total)
```

Perhaps you have the wrong kind of quote marks? Python only recognizes ASCII quote marks (either ' or "). This sometimes becomes a problem if using a rich text editors like Microsoft Word or copy/pasting code around different places.

# SyntaxError

total = 0
for line in open(’/home/values’):
    total = total + int(line.replace(’,’,’’))
print(total)

#############################

# Works fine for me

total = 0
for line in open('/home/values'):
    total = total + int(line.replace(',',''))
print(total)
1 Like

Hi Prabir, and welcome!

If you are running code in the interactive interpreter, at the >>>
prompt, you must end an indented block with a blank line:

>>> for i in (1, 2):
...     print(i)
... print('done')
  File "<stdin>", line 3
    print('done')
    ^
SyntaxError: invalid syntax

But this will work correctly:

>>> for i in (1, 2):
...     print(i)
... 
1
2
>>> print('done')
done

You only need that blank line in the interactive interpreter. If you
save your code to a .py file, and run the code, it should run okay (if
there are no other errors).

1 Like

Thanks Dennis. Taking clue from your response I figured out that copy paste is creating issues even though the code that ran and the one that didn’t looked exactly same! So, sometimes while copying and pasting its not always WUSIWUG!

Hi Stephen, thanks to your input I could figure this thing. Indeed, as you pointed out it’s different in the interpretor. As you said, when I ran it as a Python script all was good.
One twist in the tale though. When I simply copied over the code from my terminal to a .py script it gave the same error!! But when I rewrote those very three line in the same way it ran fine :slight_smile:

It would be interesting to know what your copy/paste process was.

When I examined the code in your first post in this discussion it
definitely had nonASCII quote marks. But, of course, being even further
removed from it than you are, I couldn’t even know if that had happened
when you made the post rather than when you made the code.

Sounds like it happened when you made the code.

What did you copy the text from in your first copy/paste? I’ve certainly
had issues pulling from textbooks or course materials, whose quotes got
mangled when they were rendered. I once even had to teach from course
materials whose printed form had visibly mangled quotes - I suppose at
least the damage was apparent.

Cheers,
Cameron Simpson cs@cskk.id.au

In Prabir’s original post, as sent by email, there is a HTML part and a
plain text part.

The plain text part has ASCII quotes, the HTML part has fancy quotes.

My emailer is configured to view the plain text part in preference if it
is available. If you are seeing fancy Unicode quotes, you are probably
seeing the HTML part.

My wild guess is that Prabir created his post using the fancy text input
widget, and tried to use manually-entered markdown code (triple
back-ticks) to flag the code block. But markdown only works if you are
in the markdown editor, not the fancy editor. And my guess is that the
fancy editor converts ASCII quotes to some sort of Unicode quote.

If there is anyone posting from the website, rather than replying by
email, can you confirm that the text entry widget behaves like this?

In Prabir’s original post, as sent by email, there is a HTML part and a
plain text part.

The plain text part has ASCII quotes, the HTML part has fancy quotes.

Thought I’d looked at those. On reexamination, clearly not.

My emailer is configured to view the plain text part in preference if it
is available. If you are seeing fancy Unicode quotes, you are probably
seeing the HTML part.

My mailer is configured the same as yours in this regard. I think I
mis-examined the source message.

Cheers,
Cameron Simpson cs@cskk.id.au