SyntaxError: invalid syntax for a beginner

SyntaxError: invalid syntax for a beginner

嗨,我已经在 java 编程几个月了,我有一些时间休息所以我想尝试一些 python 所以我决定尝试写我的 java 程序变成 python 但我终究无法弄清楚这段代码哪里出错了。我想在这个程序中使用 for 循环和 while 循环只是为了练习,但我一直收到错误

这是我的代码:

breakLine = "\n-------------------------------------------------\n"

print breakLine

startReading = float(raw_input("Please enter the odometer start reading in Miles "))
endReading = float(raw_input("Now please enter the odometer end reading in Miles "))
totalMiles = endReading - startReading
totalGal = 0.0

gals = [];
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];

for i in range (0, 5):
    gals.insert(i, float(raw_input("Enter gals for " + days[i] + " ")))
    totalGal += gals[i]
    i += 1

avgFuel = totalMiles / totalGal

print breakLine
print "Below is some information about your weeks travel"
print breakLine

print ("{0:20} \t {1:20}".format("DAY", "GALLONS USED"))
print breakLine

x = 0

while x < len(days):
    print ("{0:20} \t {1:20}".format(days[x], str(gals[x]))
    x += 1

print breakLine
print "You used a total of:", totalGal, "gallons this week"
print "You travelled a total of:", totalMiles, " Miles this week"
print "Your average fuel consumption for the week is:", avgFuel, "MPG"

这是我得到的错误

  File "Week1-2.py", line 31
    x += 1
    ^
SyntaxError: invalid syntax

任何帮助都将是很好的

您漏掉了打印末尾的括号

x = 0
while x < len(days):
    print ("{0:20} \t {1:20}".format(days[x], str(gals[x])))
    x += 1

为了避免这些类型的问题,我建议使用 IDE,例如 pycharm,它将帮助您轻松发现错误。