Python将函数值返回到另一个值

时间:2015-04-07 13:43:56

标签: python function

我的Python中的一些代码存在问题。 我意识到这是一个非常懒惰的努力,但我一直试图弄清楚如何返回函数值3小时。

这只是一些代码和函数的摘录:

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返回大多数功能似乎不对,但其他任何功能都不起作用。

这个程序正常运行(即整个程序一起产生正确的计算)但是,每次在main中调用一个函数并且使用biketype时,它会重复询问这些部分的输入(行进距离等)

有没有办法只返回值而不是字符串?

1 个答案:

答案 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()放在一行上。