user2918403 user2918403 - 2 months ago 14
Python Question

Syntax error: Invalid statement python-pygame

I am learning to make a role playing game in Python by watching some tutorials on Youtube. The guy didn't show me how to setup anything to get it working. I did get pygame and stuff working by watching other videos. Anyway here is my error and code:

#!C:\python32
class Character:
def __init__(self, name, hp):
self.name = name
self.hp = hp

c = Character("Test", 5)
print c.name
print c.hp


Error:


File "C:\Users\Johnathan\Desktop\My Game\character\character.py", line 8

print c.name

^
SyntaxError: invalid syntax

[Finished in 0.2s with exit code 1]

Answer

In python3 print is a function, not a statement.

Try:

print(c.name)

Also you are missing an indentation after class Character:. (Rule of thumb: After most colons follows either a single statement on the same line, or an indented suite of statements.) Your code should read:

class Character:
    def __init__(self, name, hp):
        self.name = name
        self.hp = hp

c = Character("Test", 5)
print(c.name)
print(c.hp)