あなたが探しているのはPython Listsです。これらを使用すると、ループの実行中に新しく作成されたアイテムを追跡することができます。リストを作成するには、単純にそうようにそれを定義した:
our_bowlers = []
は、今、私たちはどちらかNone
または新規Bowler
返すために私たちのgetData
機能を変更する必要があります。
def getData():
# Get the input
our_input = input("Please enter your credentails (Name score): ").split()
# Check if it is empty
if our_input == '':
return None
# Otherwise, we split our data and create the Bowler
name, score = our_input.split()
return Bowler(name, score)
をして、私たちはループを実行することができ、新しいBowler
をチェックし、私たちが何かを取得していない場合は、私たちが作成したすべてのBowlers
を印刷することができます。
# Get the first line and try create a Bowler
bowler = getData()
# We loop until we don't have a valid Bowler
while bowler is not None:
# Add the Bowler to our list and then try get the next one
our_bowlers.append(bowler)
bowler = getData()
# Print out all the collected Bowlers
for b in our_bowlers:
print(b.nameScore())