user3631273
user3631273

Reputation: 11

how to append to a list from input data, and print the list

All I would like to do is to enter three scores for player1, and have them appended to the list please:

player1=[0,0,0]
player2=[0,0,0]

print('please enter your score for player 1, round 1')
player1.append=int(input([0]))
print('please enter your score for player 1, round 2')
player1.append=int(input([1]))
print('please enter your score for player 1, round 3')
player1.append=int(input([2]))

print(player1)

Upvotes: 0

Views: 1215

Answers (5)

Izaak van Dongen
Izaak van Dongen

Reputation: 2545

.append is a method of a list, which means you should "call" it, for example like my_list.append(item). Also, input() accepts an argument, which is the string to print before getting the input - you can do something like input("enter your name > "). Using this, it might make sense to write a reusable function that gets a score for a certain player and round:

def get_score(player, round_):
    return int(input("Enter the score for player {}, round {}> ".format(player, round_)))

The round argument is named round_ as round is already a Python builtin. Beware that this will crash if the user doesn't enter an integer, but that would take quite a bit more logic to fix. We can also write a function that collects a player's scores for a whole round:

def _get_round_scores(player, rounds):
    scores = []
    for round_ in range(rounds):
        scores.append(get_score(player, round_ + 1))
    return scores

This uses a for loop to append a score for each round (note how the logic of actually getting the score has been abstracted away). It's prefixed with an underscore as this isn't the "final" version. Using this, we might even write a similar function to get scores for different players:

def _get_player_scores(players, rounds):
    players = []
    for player in range(players):
        players.append(_get_round_scores(player + 1, rounds))
    return players

However, we can actually rewrite both without needing append at all, with Python list comprehensions (which should be well documented elsewhere), like so:

def get_round_scores(player, rounds):
    return [get_score(player, round_ + 1) for round_ in range(rounds)]

def get_player_scores(players, rounds):
    return [get_round_scores(player + 1, rounds) for player in range(players)]

Now, we can use our functions like so:

scores = player1, player2 = get_player_scores(players=2, rounds=3)
print(scores)
print(player1)
print(player2)

To get this behaviour:

Enter the score for player 1, round 1> 1
Enter the score for player 1, round 2> 2
Enter the score for player 1, round 3> 3
Enter the score for player 2, round 1> 4
Enter the score for player 2, round 2> 5
Enter the score for player 2, round 3> 6
[[1, 2, 3], [4, 5, 6]]
[1, 2, 3]
[4, 5, 6]

Upvotes: 1

arunkumarreddy
arunkumarreddy

Reputation: 177

You are trying to assing a value to list.append function. Rather you should give it as an argument to list.append function. Below code would do and is simplified one.

player1=[]
for i in range(1, 4):
    player1.append(int(input('please enter your score for player 1, round {}\n'.format(i))))
print(player1)

Upvotes: 1

Van Peer
Van Peer

Reputation: 2167

player1=[]
i=1
while i < 4:
    print('please enter your score for player 1, round ', i)
    player1.append(int(input()))
    i+=1
print(player1)

Output

please enter your score for player 1, round  1
2
please enter your score for player 1, round  2
3
please enter your score for player 1, round  3
4
[2, 3, 4]

Upvotes: 2

rain_
rain_

Reputation: 794

.append = assigns a value to append method, which is read only and its not what you want. .append() makes and actual call to append method

This can be solved with little research. Please grab a good Python book and spent few hours on it. Good luck!

my_list = []

print('please enter your score for player 1, round 1')
my_list.append(int(input([0])))
print('please enter your score for player 1, round 2')
my_list.append(int(input([1])))
print('please enter your score for player 1, round 3')
my_list.append(int(input([2])))

Upvotes: 2

Alexey
Alexey

Reputation: 409

list.append() - it's a method, so

player1=[]
player2=[]

print('please enter your score for player 1, round 1')
player1.append(int(input([0])))
print('please enter your score for player 1, round 2')
player1.append(int(input([1])))
print('please enter your score for player 1, round 3')
player1.append(int(input([2])))

print(player1)

Upvotes: 2

Related Questions