Noah Santos
Noah Santos

Reputation: 143

How to format numbers to two decimals in a f string expression?

I am trying to make this program that asks a user for their grades and displays their grades with 2 decimals. - ex. if they got a 10/15 on their Assignment it would show Assignment 1: 66.66%

In my code, it seems I am having trouble converting the numbers calculated to two decimals.

    print("Please enter your grade on the following pieces of work:")
a1 = int(input("Assignment 1 (/15):"))
a2 = int(input("Assignment 2 (/20):"))
a3 = int(input("Assignment 3 (/25):"))
a4 = int(input("Assignment 4 (/20):"))
a5 = int(input("Assignment 5 (/30):"))
t = int(input("Tutorials (/10):"))
m = int(input("Midterm (/30):"))
fe = int(input("Final Exam (/50):"))


print("Here are your grades: ")

print(f"Assignment 1:{a1/15*100}%")
print(f"Assignment 2:{a2/20*100}%")
print(f"Assignment 3:{a3/25*100}%")
print(f"Assignment 4:{a4/20*100}%")
print(f"Assignment 5:{a5/30*100}%")
print(f"Tutorials:   {t/10*100}%")
print(f"Midterm:     {m/30*100}%")
print(f"Final Exam:  {fe/50*100}%")

These answers give the whole decimal answer, for example instead of showing 66.66% it would show 66.666667%. How would I make this into 66.66%? Thanks in advance.

Edit: Also how would I take these answers and create the average percentage?

Upvotes: 3

Views: 2590

Answers (3)

Acccumulation
Acccumulation

Reputation: 3591

You probably should ask your second question as a separate question, but one implementation would be to create a pandas dataframe.

 import pandas as pd
 last_assignment_number = 5
 assignments = ['Assignment {}'.format(i) for i in range(1,last_assignment_number+1)]+
                 ['Tutorials','Midterm','Final Exam']
 scores = pd.DataFrame(index=assignments+['Total'],
                 columns=['score','maximum','percentage'])
 maximum_scores = [15,20,25,20,30,10,30,50]
 scores['maximum'] = maximum_scores+[sum(maximum_scores)]
 for assignment in assignments:
      scores.loc[assignment,'score']=int(input("{} (/{}):").format(assignment,scores.loc[assignment,'maximum'])
 scores.loc['Total','score']=scores.loc[assignments,'score'].sum()
 scores['percentage']=(scores['score']/scores['maximum']).apply(lambda x:f"{x:.2%}")

Upvotes: 0

Julian Camilleri
Julian Camilleri

Reputation: 3105

You can use the following syntax to denote that you want the formatting in two decimal points. - You can read more here.

{your_calculation:.2%}

The .2 in this case denotes that you want the formatting in two decimal points. If you want to increase or decrease the decimal points you can adjust accordingly.

Upvotes: 4

hiro protagonist
hiro protagonist

Reputation: 46849

python supports percent as formatting option:

a3 = 7
print(f"Assignment 3:  {a3/25:.2%}")  # 'Assignment 3:  28.00%'

note that there is no need to divide by 100 when using % as format specifier.

search form percent in the Format Specification Mini-Language. more information on formatting can be obtained here: https://pyformat.info/.

Upvotes: 3

Related Questions