gpt4 book ai didi

python - 如何在 Python 中计算百分比

转载 作者:行者123 更新时间:2023-12-03 23:58:34 25 4
gpt4 key购买 nike

我正在用 Python 编写一个程序来确定我在各个科目中的分数百分比。它应该问我的分数并找到百分比,但有问题。我还很年轻,所以请不要 mock 我的尝试:

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)

但最后它说

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

我在这里做错了什么?

最佳答案

input 函数返回一个字符串。在计算 english + maths + science/3 时,您正在添加字符串!您应该通过 int(string)float(string) 函数将输入转换为数字。您还可以使用 'mystring'.isdigit() 来检查您的字符串是否由数字组成。

关于python - 如何在 Python 中计算百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66208997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com