Python-如何按顺序调用列表中的下一个玩家

标签 python list

我在如何按顺序实际调用列表中的下一个玩家时遇到一些问题。

基本上,我需要从包含玩家姓名的 p_record LIST 中进行检查。

    def main():

    attempts = 0
    white = 0
    wrong = 0
    score = 0
    black = 0 
    game = True    
    p_record = []
    whiteblack = []
    colors = ['B', 'R', 'Y', 'O', 'G', 'P']
    color_code = random.sample(colors, 4)         
    print ("HIDDEN CODE", color_code)
    num = int(input('Please enter number of players: '))
    for i in range(num):
             names = input('Please enter a name: ')
             p_record.append([names, 0, 0])
             print(p_record)
             print(len(p_record))
    print(names + ",", "make a guess of 4 colors from RGBYOP: ")          
    player_guess = input("").upper()               
    for i in range(len(player_guess)):                     
             if player_guess[i] == color_code[i]:
                      black += 1
                      score += 5
                      whiteblack.append("B")
             if player_guess[i] != color_code[i] and player_guess[i] in color_code:
                      white += 1
                      score += 1
                      whiteblack.append("W")

             else:
                      whiteblack.append("")

    color_codeString = ''.join(color_code)                      
    whiteblackString = ''.join(whiteblack)              
    print("Result", whiteblackString)
    print("Current player:", names, "Current Score:", score % score)                                  
    print("Current player:", names, "Updated Score:", score)
    if(player_guess == color_codeString):
             print("Winner: ", p_record)            


main()

这就是我想要的结果。

Enter number of players: 3

Enter player's name: alan

Enter player's name: betty

Enter player's name: cindy Alan, make a guess of 4 colors from RGBYOP: BYOP

Result WWB

Current Player: Alan Current Score: 0

Current Player: Alan Updated Score: 7

Betty, make a guess of 4 colors from RGBYOP: BYOR

Result WB

Current Player: Betty Current Score: 0 Current Player: Betty Updated Score: 0

Cindy, make a guess of 4 colors from RGBYOP: BYPG

Result WWWB Current Player: Cindy Current Score: 0 Current Player: Cindy Updated Score: 1

Alan, make a guess of 4 colors from RGBYOP: BPGY

Result BBBB Current Player: Alan Current Score: 7 Current Player: Alan Updated Score: 22

Correct guess!

[['Alan', 22, 2], ['Betty', 0, 0], ['Cindy', 1, 0]] Winner: ['Alan', 22, 2]

最佳答案

您只需循环遍历您的p_record 列表即可。这是一个简单的例子。

p_record = [
    ['Alan', 0, 0],
    ['Betty', 0, 0],
    ['Cindy', 0, 0],
]

game = True
while game:
    for player in p_record:
        print(player)
        player[1] += 1
        if player[1] > 3:
            game = False
            break

输出

['Alan', 0, 0]
['Betty', 0, 0]
['Cindy', 0, 0]
['Alan', 1, 0]
['Betty', 1, 0]
['Cindy', 1, 0]
['Alan', 2, 0]
['Betty', 2, 0]
['Cindy', 2, 0]
['Alan', 3, 0]

您可能还会发现此处的示例很有帮助:Asking the user for input until they give a valid response .

关于Python-如何按顺序调用列表中的下一个玩家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52065898/

相关文章:

python - 如何在 Django 的多个同时进行的 Keras 分类器 session 中进行预测?

Python:添加列表的每隔一个条目(初学者)

python - 从python中的列表中提取数组

arrays - Groovy,如何使用索引迭代列表

python - 如何在C/C++程序中加载内核模块

python - 在 OR 工具中通过 SWIG 使用 Python 回调

Python 编码约定“ block : found by pylint 之前的错误继续缩进

python - 在python中获取一组二维列表

jQuery 为某些元素添加类

Python切片列表中的第一个和最后一个元素