【问题标题】:how to calculate percentage in python如何在python中计算百分比
【发布时间】:2014-03-04 10:19:22
【问题描述】:

这是我的程序

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 中正确计算百分比?

【问题讨论】:

  • 不相关:如果您有分数,则将其打印为百分比:print "{:.0%}".format(sum(marks) / (len(marks) * perfect_mark * 1.0))

标签: python python-2.7 percentage


【解决方案1】:

我猜你正在学习如何使用 Python。其他答案都是对的。但我要回答你的主要问题:“如何在 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))

Here你可以测试代码并进行实验。

【讨论】:

    【解决方案2】:

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

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

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

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

    per= tota*100.0 / 500
    

    【讨论】:

      【解决方案3】:

      这是因为(100/500) 是一个产生 0 的整数表达式。

      试试

      per = 100.0 * tota / 500
      

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

      【讨论】:

      • @unwind 关于float 显式转换,您是对的。我只是包括在内,因为复制粘贴了 OP 的行。修正我的答案。
      【解决方案4】:

      对我有用的百分比计算:

      (new_num - old_num) / old_num * 100.0
      

      【讨论】:

        【解决方案5】:
        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() 函数用于从控制台获取输入及其返回字符串格式化值。所以我们需要转换成整数,否则会导致转换错误。

        【讨论】:

          【解决方案6】:

          我知道我迟到了,但如果你想知道最简单的方法,你可以编写这样的代码:

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

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

          【讨论】:

            【解决方案7】:
            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 %
            

            【讨论】:

              【解决方案8】:

              #刚开始我的 Python 编码生涯 #这就是我写的很简单

              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)
              

              【讨论】:

                【解决方案9】:

                您可以使用部分 == 总分 == 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)
                

                【讨论】:

                  猜你喜欢
                  • 2022-06-23
                  • 2021-04-12
                  • 2017-09-16
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-02-15
                  • 1970-01-01
                  • 2021-05-26
                  相关资源
                  最近更新 更多