Python Forum

Full Version: Python random formatting issues
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello all -

Im occasionally running into a problem where Python will just ignore a formatting command, but the work fine on similar elements.

For example this part of the code is taking multiple lists and pretty printing them:
Delivery_Type = [
        "CMR - Construction Management at Risk",
        "DBB - Design Bid Build",
        "DB - Design Build",
        "MP - Multi-Prime",
        "IPD - Integrated Project Delivery,"]

Fee_Type = 
        "HOURLY",
        "HOURLY NTE",
        "FIXED FEE",
        "PERCENTAGE",
        "INVESTMENT",
        "PRO_BONO"]

print("""
Time to look at the Project Delivery Method.
Please choose from one of these: """)
    pp.pprint(Delivery_Type)
    Project_Delievery = input("What is the Project Delivery Type?: ").upper()
  

    print("""
Let's look at the FEE structure now.
Please choose from one of these: """)
    pp.pprint(Fee_Type)
    Fee = input("What type of Fee will this project be?: ").upper()
It runs fine except only one out of the several similar list wont print pretty, they all have the same formatting the same syntax the only thing different are the words used.

Time to look at the Project Delivery Method.
Please choose from one of these: 
[    'CMR - Construction Management at Risk',
     'DBB - Design Bid Build',
     'DB - Design Build',
     'MP - Multi-Prime',
     'IPD - Integrated Project Delivery,']
What is the Project Delivery Type?: a

Let's look at the FEE structure now.
Please choose from one of these: 
['HOURLY', 'HOURLY NTE', 'FIXED FEE', 'PERCENTAGE', 'INVESTMENT', 'PRO_BONO']
What type of Fee will this project be?: 
I have another instance were I try to run a ceiling format command and it works for all but one.
Rather than using pp, use format:
def formatted_print():
    delivery_type = ["CMR - Construction Management at Risk", "DBB - Design Bid Build",
                     "DB - Design Build", "MP - Multi-Prime", "IPD - Integrated Project Delivery,"]

    fee_type = ["HOURLY", "HOURLY NTE", "FIXED FEE", "PERCENTAGE", "INVESTMENT", "PRO_BONO"]

    for type, fee in zip(delivery_type, fee_type):
        type = type.split('-')
        print('{0:<4}-{1:<40}{2}'.format(type[0], type[1], fee))

if __name__ == '__main__':
    formatted_print()
output:
Output:
CMR - Construction Management at Risk HOURLY DBB - Design Bid Build HOURLY NTE DB - Design Build FIXED FEE MP - Multi PERCENTAGE IPD - Integrated Project Delivery, INVESTMENT
print('{0:<4}-{1:<40}{2}'.format(type[0], type[1], fee))
Is it possible to have width (in this case 4 and 40) as a variable, so it can vary depending on text length?
I checked the docs but didn't see such example.
(Jan-24-2018, 06:54 PM)j.crater Wrote: [ -> ]Is it possible to have width (in this case 4 and 40) as a variable, so it can vary depending on text length?
Here is an eight years old example of variable width and precision
rabbits = {
    "flopsy" : 1.0/3, "mopsy" : 576.0/7, "cotton tail": .76/5, "peter": 300000.0/37,
}

nwidth = 1 + max(len(name) for name in rabbits)

for name in sorted(rabbits):
    # the floating point precision is passed as argument to format
    print("{name:{namewidth}}:{score:>10.{precision}f}".format(
          name = name, score = rabbits[name],
          namewidth = nwidth, precision = 2))

"""my output ---->
cotton tail :      0.15
flopsy      :      0.33
mopsy       :     82.29
peter       :   8108.11
"""
Just what I wondered about, thanks! :D
(Jan-24-2018, 07:00 PM)Gribouillis Wrote: [ -> ]Here is an eight years old example of variable width and precision
I remember your good post about string formatting when it was new in Python 2.6 Cool
(Jan-24-2018, 09:33 PM)snippsat Wrote: [ -> ]when it was new in Python 2.6
Yes, well, at that time I was learning the format() method. I thought there was much more to come about it, but this proved untrue. Who really uses the __format__ attribute? The thread that you linked contains mostly beginner's experimentation. Rolleyes
Thanks guys for the quick replies i'll definitely use this information,
But its also not quite the issue i'm running into. Its that on two occasions when I have programmed something that involves formating a string or a list, it will work on 4 out of the 5 strings but there's one that just ignores it, everything is the same between them but it just doesn't change.

So for example one script I have 6 calculations that each returns a float and I want to round down so I use a round command.
It will work on 5 of them but the last one just returns the same number.

def dms_calc(DMS,DMS_Cost, Cost_Opinion):
    import math
    if DMS == "A":
        A = (0.187547-(0.01836*(math.log10(DMS_Cost/7.76))))
        A = round(A,4)
        print(A*100, "%")
        print("$",DMS_Cost*A)
    elif DMS == "B":
        B = (0.164145-(0.015303*(math.log10(DMS_Cost/7.76))))
        B = round(B,4)
        print(B*100, "%")
        print("$",DMS_Cost*B)
    
    elif DMS == "C":
        C = (0.142432-(0.010594*(math.log10(DMS_Cost/7.76))))
        C = round(C,4)
        print(C*100, "%")
        print("$",DMS_Cost*C)
    elif DMS == "D":
        D = (0.141419-(0.01236*(math.log10(DMS_Cost/7.76))))
        D = round(D,4)
        print(D*100, "%")
        print("$",DMS_Cost*D)
    elif DMS == "E":
        E = (0.118011-(0.009279*(math.log10(DMS_Cost/7.76))))
        E = round(E,4)
        print(E*100, "%")
        print("$",DMS_Cost*E)
    elif DMS == "F":
        F = (0.09521-(0.006301*(math.log10(DMS_Cost/7.76))))
        F = round(F,4)
        print(F*100, "%")
        print("$",DMS_Cost*F)
and the with the script I was talking about at the start it does the same were it will work on 80% of the lists but it just ignores some of them. It just seems like a bug and was wondering if anyone else had experience with something like this.
(Jan-28-2018, 09:03 PM)Barnettch3 Wrote: [ -> ]It just seems like a bug and was wondering if anyone else had experience with something like this.

If there is a bug, it is in your code, it is not in the python interpreter. The round function does exactly what the documentation says.

If you are experimenting errors with formatting, then you need the format method. For example the line
print(F*100, "%")
is better written
print('{:.2f} %'.format(F*100))
Can you describe the issue more precisely or give a code that we can run to see the pathological behavior?
(Jan-28-2018, 09:23 PM)Gribouillis Wrote: [ -> ]
(Jan-28-2018, 09:03 PM)Barnettch3 Wrote: [ -> ]It just seems like a bug and was wondering if anyone else had experience with something like this.
If there is a bug, it is in your code, it is not in the python interpreter. The round function does exactly what the documentation says. If you are experimenting errors with formatting, then you need the format method. For example the line
print(F*100, "%")
is better written
print('{:.2f} %'.format(F*100))
Can you describe the issue more precisely or give a code that we can run to see the pathological behavior?

Thank you that works but im just trying to understand why it would give me this problem in the first place, if you look at item B you can see what I mean.
def dms_calc():
    import math
    DMS = input("Choose DMS Letter A-F").strip().upper()
    DMS_Cost = 250000
    if DMS == "A":
        A = (0.187547-(0.01836*(math.log10(DMS_Cost/7.76))))
        A = round(A,4)
        print(A*100, "%")
        print("$",DMS_Cost*A)
    elif DMS == "B":
        B = (0.164145-(0.015303*(math.log10(DMS_Cost/7.76))))
        B = round(B,4)
        print(B*100, "%")
        print("$",DMS_Cost*B)
    
    elif DMS == "C":
        C = (0.142432-(0.010594*(math.log10(DMS_Cost/7.76))))
        C = round(C,4)
        print(C*100, "%")
        print("$",DMS_Cost*C)
    elif DMS == "D":
        D = (0.141419-(0.01236*(math.log10(DMS_Cost/7.76))))
        D = round(D,4)
        print(D*100, "%")
        print("$",DMS_Cost*D)
    elif DMS == "E":
        E = (0.118011-(0.009279*(math.log10(DMS_Cost/7.76))))
        E = round(E,4)
        print(E*100, "%")
        print("$",DMS_Cost*E)
    elif DMS == "F":
        F = (0.09521-(0.006301*(math.log10(DMS_Cost/7.76))))
        F = round(F,4)
        print(F*100, "%")
        print("$",DMS_Cost*F)
>>> dms_calc()
Choose DMS Letter A-FA
10.48 %
$ 26200.0
>>> dms_calc()
Choose DMS Letter A-FB
9.520000000000001 %
$ 23800.0
>>> dms_calc()
Choose DMS Letter A-FC
9.47 %
$ 23675.0
>>> dms_calc()
Choose DMS Letter A-FD
8.57 %
$ 21425.0
>>> dms_calc()
Choose DMS Letter A-FE
7.62 %
$ 19050.0
>>> dms_calc()
Choose DMS Letter A-FF
6.68 %
$ 16700.0
>>> 
Pages: 1 2