语法错误:初学者的语法无效



嗨,我已经用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

要解决这些类型的问题,我建议使用像pycharm这样的IDE,它将帮助您轻松找到错误。

最新更新