2015-10-14 96 views
0

我在一个非常基本的介绍级编程课程,我正在学习函数。我的代码不停地将调用返回到我的程序结尾处的“main()”作为语法错误。我又是超级坏在这所以不要评判我所有的错误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() 

当我运行它,它与主高亮说语法错误

+0

你'而速度<0:'语句缩进一个水平向右 – karthikr

+0

看起来像缩进问题,... – alfasin

+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")) 
+0

只要我想出如何大声笑 –

+0

它说我必须等待一分钟 –

+0

你现在应该可以做到了。 – intboolstring