'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 do I calculate the percentage correctly in Python?
Solution 1:[1]
You're performing an integer division. Append a .0 to the number literals:
per=float(tota)*(100.0/500.0)
In Python 2.7 the division 100/500==0.
As pointed out by @unwind, the float() call is superfluous since a multiplication/division by a float returns a float:
per= tota*100.0 / 500
Solution 2:[2]
This is because (100/500) is an integer expression yielding 0.
Try
per = 100.0 * tota / 500
there's no need for the float() call, since using a floating-point literal (100.0) will make the entire expression floating-point anyway.
Solution 3:[3]
Percent calculation that worked for me:
(new_num - old_num) / old_num * 100.0
Solution 4:[4]
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 : raw_input() function is used to take input from console and its return string formatted value. So we need to convert into integer otherwise it give error of conversion.
Solution 5:[5]
I know I am late, but if you want to know the easiest way, you could do a code like this:
number = 100
right_questions = 1
control = 100
c = control / number
cc = right_questions * c
print float(cc)
You can change up the number score, and right_questions. It will tell you the percent.
Solution 6:[6]
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 %
Solution 7:[7]
#Just begining my coding career with Python #here's what i wrote its simple
print("\nEnter your marks to calculate percentage")
a=float(input("\nEnter your English marks"))
b=float(input("\nEnter your Mathematics marks"))
c=float(input("\nEnter your Science marks"))
d=float(input("\nEnter your Computer Science marks"))
e=float(input("\nEnter your History marks"))
tot=a+b+c+d+e
print("\nTotal marks obtained",tot)
per=float(tot/500)*100
print("Percentage",per)
Solution 8:[8]
You can try the below function using part == total marks out of whole == 500.
def percentage(part, whole):
try:
if part == 0:
percentage = 0
else:
percentage = 100 * float(part) / float(whole)
return "{:.0f}".format(percentage)
except Exception as e:
percentage = 100
return "{:.0f}".format(percentage)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | sridhar |
| Solution 4 | DigitalGeek |
| Solution 5 | Mr. Code |
| Solution 6 | Yash Shukla |
| Solution 7 | Azam Siddiqui |
| Solution 8 | Wasim Shaikh |
