如何在python中计算百分比

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22169081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:24:34  来源:igfitidea点击:

how to calculate percentage in python

pythonpython-2.7percentage

提问by

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?

但是,百分比结果是0。如何在 Python 中正确计算百分比?

采纳答案by JJGuerrero

I guess you're learning how to Python. The other answers are right. But I am going to answer your main question: "how to calculate percentage in python"

我猜你正在学习如何使用 Python。其他答案都对。但我要回答你的主要问题:“如何在 python 中计算百分比”

Although it works the way you did it, it doesn′t look very pythonic. Also, what happens if you need to add a new subject? You'll have to add another variable, use another input, etc. I guess you want the average of all marks, so you will also have to modify the count of the subjects everytime you add a new one! Seems a mess...

虽然它按照你的方式工作,但它看起来不是很 Pythonic。另外,如果您需要添加新主题会怎样?您必须添加另一个变量,使用另一个输入等。我猜您想要所有分数的平均值,因此每次添加新主题时,您还必须修改主题的数量!好像乱七八糟...

I′ll throw a piece of code where the only thing you'll have to do is to add the name of the new subject in a list. If you try to understand this simple piece of code, your Python coding skills will experiment a little bump.

我将抛出一段代码,您唯一需要做的就是在列表中添加新主题的名称。如果你试图理解这段简单的代码,你的 Python 编码技能将会经历一些小插曲。

#!/usr/local/bin/python2.7

marks = {} #a dictionary, it's 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))

Hereyou can test the code and experiment with it.

您可以在此处测试代码并对其进行试验。

回答by Paulo Bu

You're performing an integer division. Append a .0to the number literals:

您正在执行整数除法。将 a 附加.0到数字文字:

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

In Python 2.7 the division 100/500==0.

在 Python 2.7 中,除法100/500==0.

As pointed out by @unwind, the float()call is superfluous since a multiplication/division by a float returns a float:

正如@unwind 所指出的,这个float()调用是多余的,因为一个浮点数的乘法/除法返回一个浮点数:

per= tota*100.0 / 500

回答by unwind

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

这是因为(100/500)是一个产生 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.

不需要float()调用,因为使用浮点文字 ( 100.0) 无论如何都会使整个表达式成为浮点。

回答by DigitalGeek

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.

注意:raw_input() 函数用于从控制台获取输入及其返回字符串格式的值。所以我们需要转换成整数,否则会产生转换错误。

回答by sridhar

Percent calculation that worked for me:

对我有用的百分比计算:

(new_num - old_num) / old_num * 100.0

回答by Mr. Code

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.

您可以更改数字分数和 right_questions。它会告诉你百分比。

回答by Yash Shukla

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 %