How to calculate percentages in Python

Viewed 1828

I am making a program in Python to determine the percentage of my marks in various subjects. It is supposed to ask me for my marks and find the percentage but something is wrong. I am rather young, so please don't laugh at my attempt:

import os
import time
print ("This is a program to calculate your percentage in your ACADEMICS ")
time.sleep(2)

english = input("Enter your english marks \n \t")
time.sleep(1)

maths = input ("Enter your maths marks \n \t")
time.sleep(1)

science= input ("Enter your science marks \n \t")
time.sleep(1)

print ("OK, i am reviewing your marks, ")
time.sleep(1)
print ("Hmm, verry poor marks, but here is your %")

percentage = english + maths + science /3
print(percentage)

but in the end it says that

Traceback (most recent call last):
  File "C:/Users/COOL BOY/Desktop/percentage.py", line 18, in <module>
    percentage = english + maths + science %3
TypeError: not all arguments converted during string formatting

What am I doing wrong here?

5 Answers

The input function returns a string. When computing english + maths + science /3 you are adding strings ! You should convert your inputs to numbers via the int(string) or float(string) functions. You can also check whether your strings is made of digit with 'mystring'.isdigit().

Convert string to int when asking for inputs, when calculating you are using strings instead of ints.

import os
import time
print ("This is a program to calculate your percentage in your ACADEMICS ")
time.sleep(2)


english = int(input("Enter your english marks \n \t"))
time.sleep(1)

maths = int(input("Enter your maths marks \n \t"))
time.sleep(1)


science= int(input("Enter your science marks \n \t"))
time.sleep(1)


print ("OK, i am reviewing your marks, ")
time.sleep(1)
print ("Hmm, verry poor marks, but here is your %")

percentage = (english + maths + science) /3
print(percentage)
  1. Input should be casted by int Ex: int(input("number"))

  2. Considering MaxScore for a subject is 100 the formula would be

    percentage=(aquaired marks of all 3 subjects)x100/(3*100)

  3. Percentage should be rounded up by last 2 digits.

The final program would be.

import os
import time

maxscore = 100
totalsubjects=3

print ("This is a program to calculate your percentage in your ACADEMICS ")
time.sleep(2)


english = int(input("Enter your english marks \n \t"))
time.sleep(1)

maths = int(input("Enter your maths marks \n \t"))
time.sleep(1)


science= int(input("Enter your science marks \n \t"))
time.sleep(1)


print ("OK, i am reviewing your marks, ")
time.sleep(1)
print ("Hmm, verry poor marks, but here is your %")

percentage = (english + maths + science)*100/(maxscore*totalsubjects)
print(round(percentage,2))

the pythonic way

import os, time

def get_subject_score(sub):
    time.sleep(2)
    return int(input(f"Enter your {sub} marks : "))  # int or float as you wish

print("This is a program to calculate your percentage in your ACADEMICS")

english = get_subject_score("english")
maths = get_subject_score("math")
science = get_subject_score("science")

print(f"OK, i am reviewing your marks, {(english + maths + science) / 3}")
  1. When ur taking integrer as input it should be int(input()) instead of input() as later takes input as string
  2. To Calculate Percentage its should be Percentage = (Sum Of Aquired Marks)Max Marks/(3Max Marks)

Assuming 100 As Marks *Percentage = (english + maths + science)100/(3*100)

Related