I am looking to create instances of a class from user input

前端 未结 2 872
离开以前
离开以前 2021-01-22 23:01

I Have this class:

class Bowler:

    def __init__(self, name, score):
        self.name = name
        self.score = scor         


        
2条回答
  •  囚心锁ツ
    2021-01-22 23:46

    This is my code to do what you want:

    class Bowler:
        def __init__(self, name, score):
            self.name = name
            self.score = score
    
        def nameScore(self):
            return '{} {}'.format(self.name, self.score)
    
    def getData():
        try:
            line = input("Please enter your credentails (Name score): ")
        except SyntaxError as e:
            return None
        name, score = line.split()
        score = int(score)
        B = Bowler(name, score)
        print(B.nameScore())
        return B
    
    if __name__ == '__main__':
        bowlers = list()
        while True:
            B = getData()
            if B == None:
                break
            bowlers.append(B)
    
        for B in bowlers:
            print(B.nameScore())
    

    In addition, I recommend you to modify your input for it's inconvenient now

提交回复
热议问题