如何将参数传递给函数,在不同的时间?

时间:2014-04-26 08:47:09

标签: python for-loop indexing argument-passing

我需要将两个不同的参数传递给代码(cartype和bonus)并设置一个for循环,这样每次循环时都会显示不同类型的汽车。不同的奖金额。

我在这里寻找了几个解决方案,在google&在我的教科书中没有任何成功。我知道将它们全部分开制作会更容易,但这就是所需要的。

任何帮助都是幻想:)请在下面找到我的部分代码。

def main():

#This program uses a while loop, to give the user the option to go again.
while True:

    #This displays the heading & instructions.
    print ('AUSSIE BEST CAR INCOME AND BONUS CALCULATOR')
    print ('*********************************')
    print('**Please input data in the following format: 10000 **')
    print('**Please be aware that inputs of zero(0) are invalid**')

    #The program asks the user to input the total price of the cars, one by one.
    kluger_price = float(input('Please enter the selling price of the Toyota Kluger: $'))
    patrol_price = float(input('Please enter the selling price of the Nissan Patrol: $'))
    territory_price = float(input('Please enter the selling price of the Ford Territory: $'))

    #The program asks the user to input the total amount of cars sold, one by one.
    kluger_sold = int(input('Please enter the number of Toyota Klugers sold in 2014:'))
    patrol_sold = int(input('Please enter the number of Nissan Patrols sold in 2014:'))
    territory_sold = int(input('Please enter the number of Ford Territorys sold in 2014:'))

    #The program calculates the totals of each of the cars, one by one.
    kluger_total = kluger_price*kluger_sold
    patrol_total = patrol_price*patrol_sold
    territory_total = territory_price*territory_sold
    total = kluger_total + patrol_total + territory_total

    #The program calculates the total bonus
    if total <= 500000:
        bonus = 0.001*total
    elif total <= 1000000:
        bonus = 500 + (total - 500000)*0.002
    elif total <= 5000000:
        bonus = 1500 + (total -1000000)*0.003
    elif total <= 10000000:
        bonus = 13500 + (total - 5000000)*0.004
    else:
        bonus = 33500 + (total - 10000000)*0.005

    #The program calculates the bonus contributed by each car
    global kluger_bonus    
    kluger_bonus = kluger_total/total*bonus
    global patrol_bonus
    patrol_bonus = patrol_total/total*bonus
    global territory_bonus
    territory_bonus = territory_total/total*bonus

    print ('*********************************')

    #The program displays the total income made and bonus    
    print ('The amount made from the Toyota Klugers is $', format(kluger_total,',.2f'))
    print ('The amount made from the Nissan Patrols is $', format(patrol_total,',.2f'))
    print ('The amount made from the Ford Territorys is $', format(territory_total,',.2f'))
    print ('The total amount amount made from all three cars is $', format(total,',.2f'))                
    print ('The total bonus for 2014 is $', format(bonus,',.2f'))
    print ('The bonus contributed through the sale of the Toyota Klugers is $', format(kluger_bonus,',.2f'))
    print ('The bonus contributed through the sale of the Nisssan Patrols is $', format(patrol_bonus,',.2f'))
    print ('The bonus contributed through the sale of the Ford Territorys is $', format(territory_bonus,',.2f'))

    cartype = ['Toyota Kluger', 'Nissan Patrol', 'Ford Territory']
    bonus = [kluger_bonus, patrol_bonus, territory_bonus]
    [CalculateAdditionalBonus(cartype, bonus) for cartype, bonus in enumerate(3)] 
    def CalculateAdditionalBonus(cartype,bonus):
        rate = input('Please input the value the assessed for the additional bonus of the', cartype')
        additional_bonus = rate*bonus
        print('The additional bonus for the', cartype' is $', format(additional_bonus,',.2f')

    def CalculateTotalBonus():


#This ends the while loop, giving the user the option to go again.
try_again = int(input('This is the end of the calculations.Press 1 to try again, 0 to exit.'))
if try_again == 0:
    break
if try_again == 1:
    print('===============================================================')
main()

2 个答案:

答案 0 :(得分:0)

我不确定我是否正确理解了您的问题,但我想您希望将cartypebonus传递到您的CalculateAdditionalBonus函数中。如果是这种情况,只需zip两个列表将它们组合成一个元组列表,然后迭代地输入您的函数。

cartype = ['Toyota Kluger', 'Nissan Patrol', 'Ford Territory']
bonus = [kluger_bonus, patrol_bonus, territory_bonus]

[CalculateAdditionalBonus(cartype, bonus) for cartype, bonus in zip(cartype, bonus)] 

答案 1 :(得分:0)

答案是使用if-elif-else语句。