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?
This question is related to
python
python-2.7
percentage
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.
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.
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.
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 %
Percent calculation that worked for me:
(new_num - old_num) / old_num * 100.0
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
Source: Stackoverflow.com