2016-04-20 6 views
1

Dies ist mein Code im Moment. Bitte beachten Sie, dass "prozentualAus" und "originalPreise" Listen mit Floats/Ints sind.Wie ordne ich meinen Python-Code so an, dass er in ein bestimmtes Format passt?

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) 

Der Ausgang ist (mit dem Rest des Codes):

 **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 

Aber ich möchte, dass die Ausgabe sein

 **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 

Wie kann ich mein Problem beheben?

+3

Druckprozentanteil in derselben Schleife wie die neue Preis – lib

+0

put 'für Prozentsatz in percentageOff [1]: print ("\ t \ t \ t", Prozentsatz, "%")' innen 'if (0 oneman

+0

Verwenden Sie ['prettytable'] (https://pypi.python.org/pypi/PrettyTable)? –

Antwort

0

Die beste Lösung, ohne eine externe Bibliothek zu verwenden, sind die verschiedenen Werte in Listen zu setzen, wenn Sie über sie iterieren und sie dann parallel drucken ... da Sie zwei verschiedene Bedingungen testen und nur eine ist muss zu einer bestimmten Zeit wahr sein.

proof of concept

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) 

Output

1   3 
2   4 
3   5 
4   6 
5   5 
34   4 
1

Abhängig von der @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") 

ausgegeben wird;

Percent off: 1  3 
       2  4 
       3  5 
       4  6 
       5  5 
       34  4 
0

Ich würde vorschlagen, Python-Formatierer für sehr gut zu verwenden! Sie Python verwenden Angenommen 3 könnte der Code auf diese Weise neu angeordnet werden (es gut kommentiert ist, so dass hoffentlich keine zusätzliche Erklärung notwendig):

#!/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() 

Dies wird eine Ausgabe wie folgt erzeugen:

        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 

Ich hoffe, das hilft.

Verwandte Themen