syntax error

I am working through the introduction to Python.

 

I am on a Macintosh. 

 

The very first project contains this line:

 

if __name__ == '__main__':

 

I get a syntax error for this, and some variations when I try to run the project. How can I fix this? Are the underscores supposed to be double underscores? I've tried both.

0
3 comments

This might have something to do with macintosh, I'm on Windows 10 and I juste copy pasted your bit of code and put a print in the if statement and it seems to work perfectly.

Or maybe, do you have any code in your if statement? if not, that would explain the syntax error. Python expects you to have a second line with a 4 spaces indentation level after this one.

0

I do have code following it. 

 

The next line, indented, is

 

my_car = Car()

as shown in the example at https://www.jetbrains.com/help/pycharm/creating-and-running-your-first-python-project.html

There are red squiggles on the If statement that say: "Unresolved reference "If" and, at the end, "End of statement expected"
and "Statement expected, found Py:COLON"

Here's my code:

If  __name__ == '__main__':
my_car = Car()
print("I'm a car!")
while True:
action = input("What should I do? [A]ccelerate, [B]rake,"
"show [o]dometer, or show average [S]peed).upper()")
If action not in "ABOS" or len(action)!=1
print("I don't know how to do that")
continue
If action == 'A'
my_car.accelerate()
elif action == 'B'
my_car.(Brake)
elif action == 'O'
print("The car has driven {} kilometers".format(my_car.odometer))
elif action == 'S'
print ("the car';s average speed was {} kpoh".format(my_car.average_speed())
my_car.step
my_car.say_state()
0

Hello, 

Do you have "If" or "if" ? The upper-case matters, please let me know if that is the case. 

0

Please sign in to leave a comment.