我写的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?
展开