简体   繁体   English

如何计算 Python 中的百分比

[英]How to calculate percentages in Python

I am making a program in Python to determine the percentage of my marks in various subjects.我正在 Python 中制作一个程序,以确定我在各个科目中的分数百分比。 It is supposed to ask me for my marks and find the percentage but something is wrong.它应该问我的分数并找到百分比,但有问题。 I am rather young, so please don't laugh at my attempt:我还很年轻,所以请不要嘲笑我的尝试:

import os
import time
print ("This is a program to calculate your percentage in your ACADEMICS ")
time.sleep(2)

english = input("Enter your english marks \n \t")
time.sleep(1)

maths = input ("Enter your maths marks \n \t")
time.sleep(1)

science= input ("Enter your science marks \n \t")
time.sleep(1)

print ("OK, i am reviewing your marks, ")
time.sleep(1)
print ("Hmm, verry poor marks, but here is your %")

percentage = english + maths + science /3
print(percentage)

but in the end it says that但最后它说

Traceback (most recent call last):
  File "C:/Users/COOL BOY/Desktop/percentage.py", line 18, in <module>
    percentage = english + maths + science %3
TypeError: not all arguments converted during string formatting

What am I doing wrong here?我在这里做错了什么?

The input function returns a string. input function 返回一个字符串。 When computing english + maths + science /3 you are adding strings !在计算english + maths + science /3时,您正在添加字符串! You should convert your inputs to numbers via the int(string) or float(string) functions.您应该通过int(string)float(string)函数将输入转换为数字。 You can also check whether your strings is made of digit with 'mystring'.isdigit() .您还可以使用'mystring'.isdigit()检查您的字符串是否由数字组成。

Convert string to int when asking for inputs, when calculating you are using strings instead of ints.在要求输入时将字符串转换为整数,在计算时使用字符串而不是整数。

import os
import time
print ("This is a program to calculate your percentage in your ACADEMICS ")
time.sleep(2)


english = int(input("Enter your english marks \n \t"))
time.sleep(1)

maths = int(input("Enter your maths marks \n \t"))
time.sleep(1)


science= int(input("Enter your science marks \n \t"))
time.sleep(1)


print ("OK, i am reviewing your marks, ")
time.sleep(1)
print ("Hmm, verry poor marks, but here is your %")

percentage = (english + maths + science) /3
print(percentage)
  1. Input should be casted by int Ex: int(input("number"))输入应由 int 强制转换 例如: int(input("number"))

  2. Considering MaxScore for a subject is 100 the formula would be考虑到一个主题的 MaxScore 是 100,公式将是

    percentage=(aquaired marks of all 3 subjects)x100/(3*100)

  3. Percentage should be rounded up by last 2 digits.百分比应按最后 2 位四舍五入。

The final program would be.最终的程序将是。

import os
import time

maxscore = 100
totalsubjects=3

print ("This is a program to calculate your percentage in your ACADEMICS ")
time.sleep(2)


english = int(input("Enter your english marks \n \t"))
time.sleep(1)

maths = int(input("Enter your maths marks \n \t"))
time.sleep(1)


science= int(input("Enter your science marks \n \t"))
time.sleep(1)


print ("OK, i am reviewing your marks, ")
time.sleep(1)
print ("Hmm, verry poor marks, but here is your %")

percentage = (english + maths + science)*100/(maxscore*totalsubjects)
print(round(percentage,2))

the pythonic way蟒蛇的方式

import os, time

def get_subject_score(sub):
    time.sleep(2)
    return int(input(f"Enter your {sub} marks : "))  # int or float as you wish

print("This is a program to calculate your percentage in your ACADEMICS")

english = get_subject_score("english")
maths = get_subject_score("math")
science = get_subject_score("science")

print(f"OK, i am reviewing your marks, {(english + maths + science) / 3}")
  1. When ur taking integrer as input it should be int(input()) instead of input() as later takes input as string当您将整数作为输入时,它应该是int(input())而不是input() ,因为稍后将输入作为字符串
  2. To Calculate Percentage its should be Percentage = (Sum Of Aquired Marks) Max Marks/(3 Max Marks)要计算百分比,它应该是百分比 =(获得标记的总和)最大标记/(3最大标记)

Assuming 100 As Marks * Percentage = (english + maths + science) 100/(3*100)假设 100 作为分数 *百分比 = (英语 + 数学 + 科学) 100/(3*100)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM