Reputation: 703
in my main, all the print()
works except the last one !!
if we run this code at the end it will repeat again and start to get my input's.
and if you know any other ways to pass these function result to another one please help me.
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())
Upvotes: 0
Views: 1151
Reputation: 4664
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())
I have made several changes to your code and removed unnecessary methods.
Output:
(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
Upvotes: 0
Reputation: 947
So you have 2 problems:
Return
in a function exits the function. So putting a return in a for
loop will not work, as it will only run once and then quit the function( and for loop).
You call getMarks()
twice once by:
getIntendMark() -> sortMarks() -> getDict() -> getMarks()
and the other time by:
dictItems() -> getDict() -> getMarks()
To fix your code you will need to make some serious adjustments. A good place to start is probably Object-Oriented Programming in Python in order to get more familiar with what you are actually coding.
Upvotes: 0
Reputation: 36
You call Student.dictItems
in showAnswer()
.
In dictItems
you call getDict
.
In getDict
you iterate over Student.getMarks
, which puts you to the beginning.
I would have posted a solved code, but the whole architecture is not the way how Python considers object orientation to be.
Please read this this through! It will help you in properly designing Python classes.
The self
is, as the name indicates, a special variable just for one instance of a student. You must create an __init__(self)
function.
Upvotes: 1