2015-10-14 102 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