SyntaxError:beginn的语法无效

2024-10-01 17:26:47 发布

您现在位置:Python中文网/ 问答频道 /正文

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

这是我得到的错误

^{pr2}$

在这方面有什么帮助都很好


Tags: theinforinputrawjavafloatdays
1条回答
网友
1楼 · 发布于 2024-10-01 17:26:47

你漏掉了这篇文章末尾的括号

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

为了避免这些类型的问题,我建议使用一个像pycharm这样的IDE,它可以帮助你很容易地找到错误。在

相关问题 更多 >

    热门问题