如何从输入数据追加到列表,并打印列表

时间:2017-11-08 08:51:11

标签: python python-3.x

我想做的就是为player1输入三个分数,并将它们附加到列表中:

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)

5 个答案:

答案 0 :(得分:2)

list.append() - 它是一种方法,所以

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)

答案 1 :(得分:2)

.append =append方法赋值,该方法是只读的,而不是您想要的。 .append()append方法进行实际调用

这可以通过很少的研究来解决。请抓一本好的Python书,花几个小时就可以了。祝你好运!

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

答案 2 :(得分:2)

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

输出

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]

答案 3 :(得分:1)

您正在尝试为list.append函数赋值。相反,你应该将它作为list.append函数的参数。 下面的代码可以做到并简化为一个。

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

答案 4 :(得分:1)

.append是列表的方法,这意味着您应该“调用”它,例如my_list.append(item)。此外,input()接受一个参数,这是在获取输入之前要打印的字符串 - 您可以执行input("enter your name > ")之类的操作。使用这个,编写一个可重用的函数可能是有意义的,该函数可以获得某个玩家的得分和回合:

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

圆形参数名为round_,因为round已经是Python内置函数。请注意,如果用户没有输入整数,这将会崩溃,但这需要更多的逻辑来修复。我们还可以编写一个函数来收集玩家在整个回合中的分数:

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

这使用for循环为每一轮附加分数(注意实际获得分数的逻辑是如何被抽象掉的)。它的前缀是下划线,因为这不是“最终”版本。使用这个,我们甚至可以写一个类似的功能来获得不同玩家的分数:

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

然而,我们实际上可以在不需要append的情况下重写两者,使用Python列表推导(应在其他地方详细记录),如下所示:

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

现在,我们可以使用我们的功能:

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

要获得此行为:

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]