How to calculate percentage in python

This is my program.

print" Welcome to NLC Boys Hr. Sec. School " a=input("\nEnter the Tamil marks :") b=input("\nEnter the English marks :") c=input("\nEnter the Maths marks :") d=input("\nEnter the Science marks :") e=input("\nEnter the Social science marks :") tota=a+b+c+d+e print"Total is: ", tota per=float(tota)*(100/500) print "Percentage is: ",per 

Result

 Welcome to NLC Boys Hr. Sec. School Enter the Tamil marks :78 Enter the English marks :98 Enter the Maths marks :56 Enter the Science marks :65 Enter the Social science marks :78 Total is: 375 Percentage is: 0.0 

However, the percentage result is 0 . How to calculate percentage in Python correctly?

+8
python percentage
source share
7 answers

I think you are learning Python. Other answers are correct. But I'm going to answer your main question: "how to calculate percentage in python"

Although it works the way you did, it does not look very pythonic. Also, what happens if you need to add a new topic? You will need to add another variable, use a different input, etc. I think you want to get the average value for all brands, so you also have to change the number of items every time you add a new one! It seems a mess ...

I will throw a piece of code where the only thing you need to do is add the name of the new object to the list. If you try to understand this simple piece of code, your Python programming skills will experiment with a little kick.

 #!/usr/local/bin/python2.7 marks = {} #a dictionary, it a list of (key : value) pairs (eg. "Maths" : 34) subjects = ["Tamil","English","Maths","Science","Social"] # this is a list #here we populate the dictionary with the marks for every subject for subject in subjects: marks[subject] = input("Enter the " + subject + " marks: ") #and finally the calculation of the total and the average total = sum(marks.itervalues()) average = float(total) / len(marks) print ("The total is " + str(total) + " and the average is " + str(average)) 

Here you can test the code and experiment with it.

+11
source share

You are doing integer division. Add the number .0 to numeric literals:

 per=float(tota)*(100.0/500.0) 

In Python 2.7, the separation is 100/500==0 .

As @unwind pointed out, calling float() superfluous, since multiplying / dividing by float returns a float:

 per= tota*100.0 / 500 
+6
source share

This is because (100/500) is an integer expression giving 0.

Try

 per = 100.0 * tota / 500 

there is no need to call float() , since using a floating-point literal ( 100.0 ) will in any case make the whole floating-point expression.

+2
source share

Percentage of calculation that worked for me:

 (new_num - old_num) / old_num * 100.0 
+1
source share
 marks = raw_input('Enter your Obtain marks:') outof = raw_input('Enter Out of marks:') marks = int(marks) outof = int(outof) per = marks*100/outof print 'Your Percentage is:'+str(per) 

Note. The raw_input () function is used to enter console input and the formatted value of the returned string. Therefore, we need to convert to an integer, otherwise it will give a conversion error.

0
source share

I know I'm late, but if you want to find out the easiest way, you can make code like this:

 number = 100 right_questions = 1 control = 100 c = control / number cc = right_questions * c print float(cc) 

You can change the score of the number, and right_questions. This will tell you the percentage.

0
source share
 def percentage_match(mainvalue,comparevalue): if mainvalue >= comparevalue: matched_less = mainvalue - comparevalue no_percentage_matched = 100 - matched_less*100.0/mainvalue no_percentage_matched = str(no_percentage_matched) + ' %' return no_percentage_matched else: print('please checkout your value') print percentage_match(100,10) Ans = 10.0 % 
0
source share

All Articles