Python 访问上一个移动中的用户输入

Python 访问上一个移动中的用户输入,python,list,for-loop,Python,List,For Loop,我正在创建一个简单的游戏,它接受用户输入(0或1),并将其与计算机输入进行比较。根据用户在前几步中的输入,计算机将决定播放0或1 select_difficulty = int(input("Choose the type of game (1: Easy; 2: Difficult): ")) moves = int(input("Enter the number of moves: ")) MS = 0 PS = 0 xi = 1234 thro

我正在创建一个简单的游戏,它接受用户输入(0或1),并将其与计算机输入进行比较。根据用户在前几步中的输入,计算机将决定播放0或1

select_difficulty = int(input("Choose the type of game (1: Easy; 2: Difficult): "))

moves = int(input("Enter the number of moves: "))

MS = 0
PS = 0
xi = 1234

throw00 = []
throw01 = []
throw10 = []
throw11 = []
条件如下:

throw00=人类玩家在上一步选择0的情况下选择0的次数计数
throw01=人类玩家在上一步选择1时选择0的次数
throw10=人类玩家在其上一步为0的情况下选择1的次数
throw11=人类玩家在其上一步为1的情况下选择1的次数

可能发生以下情况: 如果玩家的最后一次掷骰是0:

如果throw10>throw00:则计算机选择1

如果throw10

如果throw10=throw00:则计算机随机选择0或1

如果玩家的最后一次投掷是1:

如果throw11>throw01:则计算机选择1

如果throw11

如果throw11=throw01:则计算机随机选择0或1。

我试图通过将玩家的移动保存在列表中,然后从该列表中访问上一个移动(一个回合后)

if select_difficulty == 2:
    for turn in range(moves):
    player_move_dif = [int(n) for n in input("Choose your move number %s (0 or 1):" % (turn+1)).split()]`
    player_previous_move = player_move_dif[1]
基于这两个变量,在这两个变量的基础上,我将玩家的移动选择(0或1)附加到相应的列表中,然后由“计算机”使用该列表来选择他的移动

if player_move_dif == 0 and player_previous_move == 0:
            throw00.append(player_move_dif)
        elif player_move_dif == 0 and player_previous_move == 1:
            throw01.append(player_move_dif)
        elif player_move_dif == 1 and player_previous_move == 0:
            throw10.append(player_move_dif)
        else:
            throw11.append(player_move_dif)
        
        #define computer behavior depening on previous player move
        if player_previous_move == 0:
                if len(throw10) > len(throw00):
                    computer_move = 1
                elif len(throw10) < len(throw00):
                    computer_move = 0
                else:
                    computer_move,xi = linear_congruence(xi) 
        elif player_previous_move == 1:
                if len(throw11) > len(throw01):
                    computer_move = 1
                elif len(throw11) < len(throw01):
                    computer_move = 0
                else:
                    computer_move,xi = linear_congruence(xi)  
        else:
            computer_move,xi = linear_congruence(xi)   

这样,当前会导致TypeError%d格式:需要数字,而不是列表。我怀疑我在错误的道路上,如何用我现在尝试的方法解决这个问题,因为我无法找到解决方案来定义我想要的变量,同时能够在以后根据需要访问它们。

此外,我不确定游戏的逻辑是否正确实现,我发现一些问题导致了您的错误

如果要将玩家的移动保存到列表中,必须在for循环之前创建一个空列表,否则会一次又一次地覆盖它。还有一个问题,第一步没有之前的动作,所以你首先需要一个起始号码:

player_move_dif = [] #create an empty list out of your for-loop

for i, turn in enumerate(range(moves)): #use enumerate() to count the loops
    if i > 0: #first move has no previous one
        player_move_dif.append(input("Choose your move number %s (0 or 1):" % (turn+1)).split()))
        player_previous_move = int(player_move_dif[i-1])
    else:
        player_move_dif.append(input("Choose your start number %s (0 or 1):" % (turn+1)).split()))
        continue #no action after the first move
然后,您可以通过移动列表的索引访问当前移动。在您的情况下,列表的最后一个索引是玩家的最后一步:

if player_move_dif == computer_move:
   MS = MS + 1
   print("player = %d machine = %d - Machine wins!" % (int(player_move_dif[-1]), computer_move)) #pick the last move from play_move_dif
   print("You: %d Computer: %d" % (PS, MS))
else:
   PS = PS + 1
   print("player = %d machine = %d - Player wins!" % (int(player_move_dif[-1]), computer_move)) #pick the last move from play_move_dif
   print("You: %d Computer: %d" % (PS, MS))

你的缩进是否是有意为“如果选择难度”
块缩进的?@ShanS:不是,我编辑了这个问题,以反映代码的缩进!现在我有一个问题,回合总是移动-1,这会导致游戏以错误的回合结束。现在我通过在循环中设置回合=1来解决这个问题。我明白了。当然,您也可以像以前一样将“%(turn+1)).split()添加到输入中
if player_move_dif == computer_move:
   MS = MS + 1
   print("player = %d machine = %d - Machine wins!" % (int(player_move_dif[-1]), computer_move)) #pick the last move from play_move_dif
   print("You: %d Computer: %d" % (PS, MS))
else:
   PS = PS + 1
   print("player = %d machine = %d - Player wins!" % (int(player_move_dif[-1]), computer_move)) #pick the last move from play_move_dif
   print("You: %d Computer: %d" % (PS, MS))