2015-04-07 37 views
-1

我在Python中遇到了一些我的代碼問題。 我意識到這是一個相當懶惰的努力,但我一直在試圖找出如何返回現在3個小時的函數值。Python將函數值返回到另一個值

這僅僅是一些代碼和功能的摘錄:

def main(): 
    another_round = 'y' 
    print (''' 
    Hawaiian Beach Bike Hire 
    ''') 
    while another_round == 'y': 
     biketype = bikeType() 
     bikeDays(biketype) 
     bikeDistance(biketype) 
     print (''' 

    days bike rent ($):''',bikeDays(biketype)) 
     print ('extra distance rent ($): ',bikeDistance(biketype)) 
     print (''' 

    total amount ($):''',bikeDistance(biketype) + bikeDays(biketype)) 
     another_round= input(''' 
    is there anymore bikes to count?''') 

def bikeType(): 
    biketype = input ('Bike type ') 
    if biketype == 'Kids'or biketype == 'kids': 
     biketype = 15 
    elif biketype == 'womans'or biketype == 'Womans': 
     biketype = 20 
    elif biketype == 'Mens'or biketype == 'mens': 
     biketype = 25 
    else: 
     print ('choose a valid bike') 
    return biketype 

    def bikeDistance(biketype): 
    if biketype == 15: 
     biked= 1.5 
    elif biketype == 20: 
     biked= 2.0 
    elif biketype == 25: 
     biked= 2.2 
    distanceRent = float(input('Distance Traveled ')) 
    bikeAdd = distanceRent * biked 
    return biketype 



main() 

它似乎並不認爲我使用biketype返回大多數功能的權利,但什麼都不起作用。

此程序功能正確(即整個程序一起產生正確的計算,是),但是每次一個函數被調用在主,並用於biketype,則重複詢問對於那些部分輸入(行進的距離等)

有沒有辦法只返回值而不是字符串?

+0

你能寫出儘可能小的程序,但仍然存在相同的問題嗎? –

回答

1

給你:

def main(): 
    another_round = 'y' 
    print ("Hawaiian Beach Bike Hire") 
    while another_round == 'y': 
     biketype = bikeType() 
     bikedays = bikeDays(biketype) 
     bikedistance = bikeDistance(biketype) 

     print ("days bike rent ($): {}".format(bikedays)) 
     print ("extra distance rent ($): {}".format(bikedistance)) 
     print ("total amount ($): {}".format(bikedistance + bikedays)) 
     another_round = input("is there anymore bikes to count?") 

的問題是你打電話bikeDays()bikeDistance()內的打印功能,以及你宣佈biketype後。爲了清楚起見,我還只是將print()放在一行上。

相關問題