Help my write this python function please...
I need to make a function that prints all people objects with a specific favorite color to the shell. Here is my code so far: class Student: def __init__(self, Name, Birthday, Color, Major, School): self.Name = Name self.Birthday = Birthday self.FavoriteColor = Color self.Major = Major self.School = School def getName(self): return self.Name def getBirthday(self): return self.Birthday def getColor(self): return self.FavoriteColor def getMajor(self): return self.Major def getSchool(self): return self.School import StudentClass def readfile(): fname = input("Enter File Name: ") infile = open(fname,"r") StudentList = [] for line in infile: data = line.split(",") OneStudent = StudentClass.Student(data[0],data[1],data[2],data[3],data[4]) StudentList.append(OneStudent) return StudentList def Color(self): color = input("Enter a Color: ") for object in StudentList: if color in StudentList: print("hello") def main(): StudentList = readfile() command = input("Enter Command Number\n 1. View all people with a specific favorite color: ") if command == "1": Color() main()
@zepdrix comp sci?
@ParthKohli @TheSmartOne @Whitemonsterbunny17 can anyone help me out with some code?
@hartnn @ganeshie8 @Kainui can better assist you :)
thank you!
def Color(self, StudentList): color = input("Enter a Color: ") for Student in StudentList: if color in getColor(): print("Yes") def Major(Self, StudentList): count=0 major=input("Enter a Major: ") for Student in StudentList: if major in getMajor(): count=count+1 print(count) what is wrong with my functions? :(
it is not valid
Python gives you a __str__ method when you write a class. You can override it if you want. Your student objects are in a list. You need to iterate through the list, check whether color matches StudentList[ x ].FavoriteColor and then prints StudentList[ x ] or not: for i in len( StudentList ): if( color == StudentList[ i ].FavoriteColor ): StudentList[ i ].__str__()
Join our real-time social learning platform and learn together with your friends!