这是我目前的代码。请注意,“percentageOff”和“originalPrices”是包含浮点数/整数的列表。
print("Percent off:", percentageOff[0], '%')
for percentage in percentageOff[1:]:
print("\t\t\t", percentage, "%")
count = 0
for prices in originalPrices:
for percentage in percentageOff:
discount = prices * (percentage/100)
newPrice = prices - discount
count += 1
if(0 < count < 11):
print("%.2f" % newPrice)
elif(11 < count < 21):
print("\t\t\t%.2f" % newPrice)
输出(使用其余代码):
**Sale Prices**
Normal Price: $9.95 $14.95 $19.95 $24.95 $29.95 $34.95 $39.95 $44.95 $49.95
______________________________________________________________________________________
Percent off: 5 %
10 %
15 %
20 %
25 %
30 %
35 %
40 %
45 %
50 %
9.45
8.96
8.46
7.96
7.46
6.96
6.47
5.97
5.47
4.97
但我希望输出为
**Sale Prices**
Normal Price: $9.95 $14.95 $19.95 $24.95 $29.95 $34.95 $39.95 $44.95 $49.95
______________________________________________________________________________________
Percent off: 5 % 9.45
10% 8.96
15% 8.46
20% 7.96
25% 7.46
30% 6.96
35% 6.47
40% 5.97
45% 5.47
50% 4.97
如何解决问题?
答案 0 :(得分:1)
取决于@danidee
x = [1,2,3,4,5,34]
y = [3,4,5,6,5,4]
print("Percent off:",end="\t")
for i, j in zip(x, y):
print(i, '\t\t', j,end="\n\t\t")
输出是;
Percent off: 1 3
2 4
3 5
4 6
5 5
34 4
答案 1 :(得分:0)
不使用外部库的最佳解决方案是在迭代它们并测试它们然后以并行方式打印它们时将不同的值放入列表中...因为您正在测试两个不同的条件且只有一个一定是真的。
概念证明
x = [1,2,3,4,5,34]
y = [3,4,5,6,5,4]
for i, j in zip(x, y):
print(i, '\t\t\t', j)
输出
1 3
2 4
3 5
4 6
5 5
34 4
答案 2 :(得分:0)
我建议使用python formatter以获得极大的好处! 假设您使用的是Python 3,代码可以通过这种方式重新排列(评论很好,所以希望不需要额外的解释):
#!/usr/bin/env python3
percentageOff = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
originalPrices = [9.95, 14.95, 19.95, 24.95, 29.95, 34.95, 39.95, 44.95, 49.95]
# Helper function
def new_price(price, off):
"""Return a new price, calculated as original price minus discount."""
return price - (price * off / 100)
## Print header
price_header = "".join(["{0}$\t".format(str(p)) for p in originalPrices])
# centre string, given the string length is 78 chars
print("{0:^78}".format("Normal Price"))
print("Off % | {0}".format(price_header))
print('-' * 78)
## Print rows
for off in percentageOff:
# padding number to 4 digits; prevent newline char at the end
print("{0:4d}% | ".format(off), end="")
for price in originalPrices:
#
print("{0:.2f}\t".format(new_price(price, off)), end="")
# print newline at the end of the row
print()
这将产生如下输出:
Normal Price
Off % | 9.95$ 14.95$ 19.95$ 24.95$ 29.95$ 34.95$ 39.95$ 44.95$ 49.95$
------------------------------------------------------------------------------
5% | 9.45 14.20 18.95 23.70 28.45 33.20 37.95 42.70 47.45
10% | 8.96 13.45 17.95 22.45 26.95 31.46 35.96 40.46 44.96
15% | 8.46 12.71 16.96 21.21 25.46 29.71 33.96 38.21 42.46
20% | 7.96 11.96 15.96 19.96 23.96 27.96 31.96 35.96 39.96
25% | 7.46 11.21 14.96 18.71 22.46 26.21 29.96 33.71 37.46
30% | 6.96 10.46 13.96 17.46 20.96 24.47 27.97 31.47 34.97
35% | 6.47 9.72 12.97 16.22 19.47 22.72 25.97 29.22 32.47
40% | 5.97 8.97 11.97 14.97 17.97 20.97 23.97 26.97 29.97
45% | 5.47 8.22 10.97 13.72 16.47 19.22 21.97 24.72 27.47
50% | 4.97 7.47 9.97 12.47 14.97 17.48 19.98 22.48 24.98
希望有所帮助。