在我的主要语言中,所有print()
都有效,最后一个除外!
如果我们在末尾运行此代码,它将再次重复并开始获取我的输入。
如果您知道将这些函数结果传递给另一方法的任何其他方法,请帮助我。
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 Student.getMarks(self):
dict[item[0]] = item[1]
return dict
def dictItems(self):
return Student.getDict(self).items()
def sortMarks(self):
marks = [x for x in Student.getDict(self).values()]
return sorted(marks)
def getIntendMark(self):
place = 0
n = 1
sort = Student.sortMarks(self)
for item in sort:
if sort[0] == sort[n]:
n += 1
else:
place = n
mark = sort[place]
return mark
def showAnswer(self):
nomre = Student.getIntendMark(self)
dict = Student.dictItems(self)
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())
答案 0 :(得分:1)
您在Student.dictItems
中呼叫showAnswer()
。
在dictItems
中,您呼叫getDict
。
在getDict
中,您遍历Student.getMarks
,这使您重新开始。
我会发布一个已解决的代码,但是整个体系结构并不是Python认为面向对象的方式。
请通读本this!它将帮助您正确设计Python类。
顾名思义,self
是一个特殊变量,仅用于一个学生实例。您必须创建一个__init__(self)
函数。
答案 1 :(得分:0)
所以您有2个问题:
Return
退出该函数。因此,将返回值置于for
循环中将不起作用,因为它只会运行一次,然后退出该函数(和for循环)。
您通过以下方式致电getMarks()
两次:
getIntendMark()-> sortMarks()-> getDict()-> getMarks()
其他时间:
dictItems()-> getDict()-> getMarks()
要修复您的代码,您将需要进行一些认真的调整。一个不错的起点可能是Object-Oriented Programming in Python,以便更熟悉您实际编写的代码。
答案 2 :(得分:0)
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