Python 如何在不同的时间将参数传递给函数?
我需要向代码传递两个不同的参数(cartype和bonus),并设置一个for循环,这样每次循环时都会显示不同类型的汽车&不同的奖金金额 我在这里,在谷歌上,在我的教科书中寻找了几种解决方案,但都没有成功。我知道单独做这些会更容易,但这是必需的 任何帮助都会很奇妙:)请在下面找到我的部分代码Python 如何在不同的时间将参数传递给函数?,python,for-loop,indexing,argument-passing,Python,For Loop,Indexing,Argument Passing,我需要向代码传递两个不同的参数(cartype和bonus),并设置一个for循环,这样每次循环时都会显示不同类型的汽车&不同的奖金金额 我在这里,在谷歌上,在我的教科书中寻找了几种解决方案,但都没有成功。我知道单独做这些会更容易,但这是必需的 任何帮助都会很奇妙:)请在下面找到我的部分代码 def main(): #This program uses a while loop, to give the user the option to go again. while True:
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()
def main():
#该程序使用while循环,为用户提供再次执行的选项。
尽管如此:
#这将显示标题和说明。
打印(“澳大利亚最佳汽车收入和奖金计算器”)
打印(“*******************************************”)
打印(“**请按以下格式输入数据:10000**”)
打印(“**请注意零(0)的输入无效**”)
#程序要求用户逐个输入汽车的总价格。
kluger_价格=浮动(输入('请输入丰田kluger的售价:$))
patrol_price=float(输入('请输入日产patrol的售价:$))
地区价格=浮动(输入('请输入福特地区的售价:$))
#该程序要求用户逐个输入汽车销售总额。
kluger_sell=int(输入('请输入2014年丰田kluger的销量:'))
patrol_selled=int(输入('请输入2014年销售的日产巡逻车数量:'))
territory_seld=int(输入('请输入2014年售出的福特territory数量:'))
#该程序逐个计算每辆车的总数。
kluger_总计=kluger_价格*kluger_售出
巡逻总数=巡逻价格*巡逻售出
地区合计=地区价格*地区销售额
总计=克鲁格总计+巡逻总计+区域总计
#该计划计算总奖金
如果total我不确定我是否正确理解了您的问题,但我猜您希望将cartype
和bonus
传递到您的calculateaditionalbonus
函数中。如果是这种情况,只需将这两个列表组合成一个元组列表,然后迭代地输入到函数中
cartype = ['Toyota Kluger', 'Nissan Patrol', 'Ford Territory']
bonus = [kluger_bonus, patrol_bonus, territory_bonus]
[CalculateAdditionalBonus(cartype, bonus) for cartype, bonus in zip(cartype, bonus)]
答案是使用if-elif-else语句。请只复制粘贴代码的相关部分。不是我感到高兴,而是你提高了获得帮助的机会。顺便说一句,我也把你的帖子标记为冒犯性的。这并没有真正回答我的问题,但谢谢你的帮助:)我已经想出了解决办法。