python从大到小排序 list_python: 怎么用list可以让user输入的variables自动排序(从大到小)?...

该程序要求用户输入鞋的价格,并根据价格从高到低排序来计算总价。目前,程序依赖用户输入的价格顺序,导致输入顺序不同结果也不同。为使程序更健壮,需要改进输入流程,允许用户随意输入价格并自动排序。通过增加一个排序步骤,可以确保无论输入顺序如何,都能正确计算总价。
摘要由CSDN通过智能技术生成

我写的3个variables必须要user从大到小输入老师说让我们设置一个排序的codeuser不需要按顺序输入也可以算出正确的价钱以下是我写的程序:num_pairs=int(raw_input("Howmanypairsofshoe...

我写的3个variables必须要user从大到小输入 老师说让我们设置一个排序的code user不需要按顺序输入也可以算出正确的价钱

以下是我写的程序:

num_pairs = int(raw_input("How many pairs of shoes to purchase? "))

if num_pairs == 2:

most_exp = raw_input("Enter the price of the most expensive pair: $ ")

sec_exp = raw_input("Enter the price of the next most expensive pair: $ ")

final = round(float(most_exp),2) + 0.5*round(float(sec_exp),2)

print "The final total is $" + str(round(final,2))

if num_pairs == 1:

most_exp = raw_input("Enter the price of the most expensive pair: $ ")

final = float(most_exp)

print "The final total is $" + str(final)

if num_pairs == 3:

most_exp = raw_input("Enter the price of the most expensive pair: $ ")

sec_exp = raw_input("Enter the price of the next most expensive pair: $ ")

least_exp = raw_input("Enter the price of the least expensive pair: $ ")

final = round(float(most_exp),2) + round(float(sec_exp),2)

print "The final total is $" + str(round(final,2))

final = round(float(most_exp),2) + 0.5*round(float(sec_exp),2) + float(least_exp)

print "If buy the third pair separate from the first 2 pairs, "

print "the final total is $" + str(round(final,2))

老师给的问题是:

One more comment on this question. Your algorithm’s correctness depends on the user entering the

prices in the correct order. Change the order, and you will see a different result. Your input routine

could be made more robust, i.e., more like a real cash register, if the prices could be entered in any

order. Of course, an extra step would be required before your algorithm could complete its calculations:

the prices need to be sorted from largest to smallest.

By exchanging the contents of variables that are out of sequence, can you enhance your input routine

so the order of the prices doesn’t matter?

展开

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值