3

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?

2
  • 1
    {a1/15*100:.2f} should do the trick for the first one. so basically add :.2f to get to decimals Commented Sep 24, 2018 at 14:41
  • Do you know how I would take these answers and calculate the final average? Commented Sep 24, 2018 at 14:46

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sweet! Thanks, this really helped. I was wondering if you know how I would find the average using these answers? I understand the math but the coding part I'm having trouble with.
How would you do it in math? :) (it will teach you more than myself just giving you the answer)
If you don't want a percent sign and you just want to show a number with two decimals places in an f-string, you can use :.2f.
3

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/.

Comments

0

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%}")

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.