SyntaxError:初学者的语法无效

时间:2015-01-18 12:09:48

标签: python syntax-error

您好我已经在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

对此的任何帮助都会很棒

1 个答案:

答案 0 :(得分:1)

您错过了在打印结尾处括起来的括号

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

为避免出现这类问题,我建议使用像pycharm这样的IDE,这样可以帮助您轻松找到错误。