Ask your own question, for FREE!
Computer Science 19 Online
OpenStudy (anonymous):

Help my write this python function please...

OpenStudy (anonymous):

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()

OpenStudy (anonymous):

@zepdrix comp sci?

OpenStudy (anonymous):

@ParthKohli @TheSmartOne @Whitemonsterbunny17 can anyone help me out with some code?

TheSmartOne (thesmartone):

@hartnn @ganeshie8 @Kainui can better assist you :)

OpenStudy (anonymous):

thank you!

OpenStudy (anonymous):

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? :(

OpenStudy (starlord1):

it is not valid

OpenStudy (rsmith6559):

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__()

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!