class StudentData:
def _init_(self, name1, marks, marks1):
self.name1 = name
self.marks = user_data
self.marks1 = user_data1
def marks_cal(self):
result = int(self.marks + self.marks1)
return result
print("Enter the name")
name = str(input())
print("Enter your marks for Maths: ")
marks = int(input())
print("Enter your marks for Chemistry: ")
marks1 = int(input())
data = StudentData(name, marks, marks1)
print("Your marks are: " %(data.marks_cal()))
您好,能否请您告诉我代码中的问题是什么。这是基本的python。 提前致谢。
答案 0 :(得分:0)
所以我可能完全错了,但我会尽力帮助。 首先让我们看一下init语句
您的代码:
class StudentData:
def _init_(self, name1, marks, marks1):
self.name1 = name
self.marks = user_data
self.marks1 = user_data1
我认为您只需要反转一些这样的代码
class StudentData:
def __inti__(self, name, user_data, user_data1):
self.name1 = name
self.marks = user_data
self.marks1 = user_data1
def marks_cal(self):
result = int(self.marks + self.marks1)
return result
应该可以正确分配用户定义的数据,并可以运行测试代码。 (您也缩进了,所以看起来像方法声明的其余部分一样)
print("Enter the name")
name = str(input())
print("Enter your marks for Maths: ")
marks = int(input())
print("Enter your marks for Chemistry: ")
marks1 = int(input())
data = StudentData(name, marks, marks1)
print("Your marks are: {}".format(data.marks_cal()))
答案 1 :(得分:0)
class StudentData:
def __init__(self, name1, marks, marks1):
self.name1 = name1
self.marks = marks
self.marks1 = marks1
def marks_cal(self):
result = int(self.marks + self.marks1)
return str(result)
print("Enter the name")
name = str(input())
print("Enter your marks for Maths: ")
marks = int(input())
print("Enter your marks for Chemistry: ")
marks1 = int(input())
data = StudentData(name, marks, marks1)
print("Your marks are: " , (data.marks_cal()))
上述代码运行正常。
1-您的代码的第一个问题是“ init ”语句。 init语句应为“ init ()”(前后加双下划线“ __”)。
2-另一个问题是代码的缩进。