[英]Trying to print(sum(list)) but getting error TypeError: unsupported operand type(s) for +: 'int' and 'str'
[英]Trying to print a value of a variable by using classes in Python. But a error pops up An error pops up unsupported operand type(s) for 'str' and 'int'
class Student:
#Static variable
school = 'vailankanni'
def __init__(self, m1 , m2, m3):
self.m1 = m1
self.m2 = m2
self.m3 = m3
def avg(self):
return (self.m1 + self.m2 + self.m3) /3
s1 = Student('20', '40', '60')
s2 = Student('80', '100', '120')
print(s1.avg())
通過打印 s1.avg() 我應該從 s1 變量中獲取詳細信息,但我得到的只是“str”和“int”彈出不支持的操作數類型的錯誤。
我應該如何解決這個問題?
您將值作為字符串對象傳遞。 而是使用
s1 = Student(20,40,60) #without the quotes
s1.avg()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.