Python返回函数值到另一个值

2024-06-28 20:52:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我的一些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时,它都会重复要求输入这些部分(行驶的距离等)

有没有办法只返回值而不返回字符串?你知道吗


Tags: or函数代码inputmaindefanotherprint
1条回答
网友
1楼 · 发布于 2024-06-28 20:52:43

给你:

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?")

问题是在print函数内部调用bikeDays()bikeDistance(),以及在声明biketype之后。为了清楚起见,我还把print()放在一行上。你知道吗

相关问题 更多 >