如何在python中计算百分比
这是我的节目如何在python中计算百分比,python,python-2.7,percentage,Python,Python 2.7,Percentage,这是我的节目 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 :")
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
结果
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
但是,百分比结果为0
。在Python中如何正确计算百分比?您正在执行整数除法。将.0
附加到数字文本:
per=float(tota)*(100.0/500.0)
在Python2.7中,除法100/500==0
正如@unwind所指出的,float()
调用是多余的,因为乘/除float返回一个float:
per= tota*100.0 / 500
这是因为(100/500)
是一个整数表达式,产生0
试一试
不需要调用
float()
,因为使用浮点文本(100.0
)将使整个表达式始终是浮点表达式。我想您正在学习如何使用Python。其他答案是正确的。但我将回答您的主要问题:“如何在python中计算百分比”
虽然它的工作方式和你做的一样,但它看起来不太像蟒蛇。另外,如果您需要添加新主题,会发生什么情况?你必须添加另一个变量,使用另一个输入,等等。我想你想要所有分数的平均值,因此每次添加新的分数时,你还必须修改科目的数量!看起来一团糟
我将抛出一段代码,您只需在列表中添加新主题的名称。如果您试图理解这段简单的代码,那么您的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))
您可以测试代码并进行实验
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)
注意:raw_input()函数用于从控制台获取输入及其返回字符串格式的值。所以我们需要将其转换为整数,否则会产生转换误差。对我有效的百分比计算:
(new_num - old_num) / old_num * 100.0
我知道我迟到了,但如果您想知道最简单的方法,可以编写如下代码:
number = 100
right_questions = 1
control = 100
c = control / number
cc = right_questions * c
print float(cc)
你可以把分数调高,然后提问。它将告诉您百分比。@unwind您对
float
显式转换的看法是正确的。我只是因为复制粘贴了OP的行而加入。修正我的答案。无关:如果你有一个分数,那么以百分比形式打印:打印“{.0%}”。格式(总和(分数)/(len(分数)*完美分数*1.0))
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 %
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 %