Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何';重新排列';我的python代码是否适合某种格式?_Python - Fatal编程技术网

我如何';重新排列';我的python代码是否适合某种格式?

我如何';重新排列';我的python代码是否适合某种格式?,python,Python,这是我目前的代码。请注意,“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 percentage

这是我目前的代码。请注意,“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 %        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

如何解决我的问题?

不使用外部库的最佳解决方案是,在对不同的值进行迭代时,将其放入列表中,并对其进行测试,然后以并行方式打印……因为您正在测试两个不同的条件,并且一次只能有一个为真

概念证明

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

取决于@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

我建议最好使用python格式化程序! 假设您使用的是Python3,代码可以这样重新排列(注释很好,希望无需额外解释):

这将产生如下输出:

                                 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

希望对您有所帮助。

在与新价格相同的循环中打印百分比Put
对于percentage in percentage Off[1:]:在
if(0
?使用?
#!/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