Python 在类中传递函数结果
在我看来,除了最后一个,所有的Python 在类中传递函数结果,python,python-3.x,class,oop,Python,Python 3.x,Class,Oop,在我看来,除了最后一个,所有的print()都能正常工作!! 如果我们在最后运行此代码,它将再次重复并开始获取我的输入。 如果你知道任何其他的方法将这些函数结果传递给另一个函数,请帮助我 我觉得不太清楚。 在showAnswer()中调用Student.dictItems 在dictems中调用getDict 在getDict中,您迭代Student.getMarks,这将使您进入开始 我本来会发布一个已解决的代码,但整个体系结构并不是Python所认为的面向对象的方式 请通读一遍!它将帮助您正
print()
都能正常工作!!
如果我们在最后运行此代码,它将再次重复并开始获取我的输入。
如果你知道任何其他的方法将这些函数结果传递给另一个函数,请帮助我
我觉得不太清楚。
在
showAnswer()中调用Student.dictItems
在dictems
中调用getDict
在getDict
中,您迭代Student.getMarks
,这将使您进入开始
我本来会发布一个已解决的代码,但整个体系结构并不是Python所认为的面向对象的方式
请通读一遍!它将帮助您正确设计Python类
正如名称所示,self
是一个特殊变量,仅用于学生的一个实例。您必须创建一个\uuuu init\uuuu(self)
函数。因此您有两个问题:
返回函数中的
退出函数。因此,将返回放入for
循环将不起作用,因为它只运行一次,然后退出函数(和for循环)
通过以下方式调用getMarks()
两次:
getIntendMark()->sortMarks()->getDict()->getMarks()
另一次是:
dictItems()->getDict()->getMarks()
要修复代码,您需要进行一些严重的调整。一个好的开始可能是为了更熟悉您实际编写的代码 这不是用Python编写类代码的方式。特别是,所有那些Student.methodName(self)
调用都应该是self.methodName()
。但是你也应该把标记之类的东西指定为实例属性。我更改了所有的方法名,但仍然有问题!我必须如何使用init,你可以这样做:class学生:def u init(self):pass#意思是:在这里什么都不做。您还可以添加self.name等变量。def getMarks(self):…
然后您可以创建一个这样的学生:student1=student()student1。getMarks()
和self
将始终是您调用方法的学生。在本例中,student1
。但是如果您正在创建和使用student28
,它也可能是student28
。您可以创建多个学生<代码>自我
自动调整。请通读这篇文章,并将我的答案标记为…@mujad我添加了一个教程链接,该教程将为您提供修复代码的基础知识。为您感到高兴!我花了相当多的时间来解决它。如果它解决了你的问题,请考虑接受它,这样问题就可以结束了。
class Student:
def getMarks(self):
number_of_students = int(input("please enter the number of students : "))
students = []
while number_of_students!=0:
name = input("please enter the name :")
mark = input("please enter the mark :")
x = [name,mark]
students.append(x)
number_of_students -= 1
return students
def getDict(self):
dict = {}
for item in self.getMarks():
dict[item[0]] = item[1]
return dict
def getIntendMark(self):
dict = self.getDict()
place = 0
n = 0 # if you had 1 student it was giving out of bound error
sort = sorted([x for x in dict.values()])
if len(sort)>1: # I check the list length
n=1
print(len(sort))
for item in sort:
if sort[0] == sort[n]:
if n < len(sort):
n += 1
else:
place = n
mark = sort[place]
return (mark, dict.items())
def showAnswer(self):
nomre,dict = self.getIntendMark()
#dict = self.getDict().items() #you are calling getMarks() 2nd times
for key,value in dict:
if value == nomre:
return f"answer:: name is {key} , mark is {nomre}"
if __name__ == "__main__":
s = Student()
# print(s.getMarks())
# print(s.getDict())
# print(s.sortMarks())
# print(s.getIntendMark())
print(s.showAnswer())
class Student:
def getMarks(self):
number_of_students = int(input("please enter the number of students : "))
students = []
while number_of_students!=0:
name = input("please enter the name :")
mark = input("please enter the mark :")
x = [name,mark]
students.append(x)
number_of_students -= 1
return students
def getDict(self):
dict = {}
for item in self.getMarks():
dict[item[0]] = item[1]
return dict
def getIntendMark(self):
dict = self.getDict()
place = 0
n = 0 # if you had 1 student it was giving out of bound error
sort = sorted([x for x in dict.values()])
if len(sort)>1: # I check the list length
n=1
print(len(sort))
for item in sort:
if sort[0] == sort[n]:
if n < len(sort):
n += 1
else:
place = n
mark = sort[place]
return (mark, dict.items())
def showAnswer(self):
nomre,dict = self.getIntendMark()
#dict = self.getDict().items() #you are calling getMarks() 2nd times
for key,value in dict:
if value == nomre:
return f"answer:: name is {key} , mark is {nomre}"
if __name__ == "__main__":
s = Student()
# print(s.getMarks())
# print(s.getDict())
# print(s.sortMarks())
# print(s.getIntendMark())
print(s.showAnswer())
(python37) C:\Users\Documents>py test.py
please enter the number of students : 1
please enter the name :e
please enter the mark :2
answer:: name is e , mark is 2