main()作为无效语法返回

时间:2015-10-14 02:37:39

标签: python-3.x

我参加了一个非常基础的入门级编程课程和我的学习功能。我的代码不断将调用返回到" main()"在我的程序结束时作为语法错误。我再次对此非常不好,所以不要判断我的所有错误

def main():
    speed = int(input('Enter the speed of the vehicle in mph: '))
    while speed < 0:
        print('Speed must be greater than zero')
        speed = int(input('Enter a valid speed: '))
    time = int(input('Enter the number of hours traveled: '))
    while time < 0:
        print('Time must be greater than zero')
        time = int(input('Enter a valid time: '))
    show_travel(speed,time)

def show_travel(speed,time):
    print('Hours\tDistance Traveled')
    print('----------------------------------------')
    for time in range(1, time + 1):
        distance = speed * time
        print(format(time, "d"), format(distance, "20.2f")
main()

当我运行它时会返回主要突出显示的语法错误

1 个答案:

答案 0 :(得分:0)

有一些括号和缩进问题。试试这个代码。对于语义,这取决于你。但是使用我的代码,没有错误抛出。

def main():
    speed = int(input('Enter the speed of the vehicle in mph: '))
    while speed < 0:
        print('Speed must be greater than zero')
        speed = int(input('Enter a valid speed: '))
    time = int(input('Enter the number of hours traveled: '))
    while time < 0:
        print('Time must be greater than zero')
        time = int(input('Enter a valid time: '))
        showtravel(speed,time)

def showtravel(speed,time):

    print('Hours\tDistance Traveled')
    print('----------------------------------------')
    for hours in range(1, hours + 1):
        distance = speed * time
        print(format(hours, "d"), format(distance, "20.2f"))