Python Dictionary如何在for循环中调用第一个Element

时间:2018-09-06 00:10:22

标签: python dictionary

我一直试图显示p_record输出。

我的问题是如何在FOR LOOP中调用p_record中的第一个元素? 该程序应调用下一个播放器,但最后却给了我相同的最后一个播放器。

def main():

attempts = 0
white = 0
wrong = 0
black = 0 
game = True    
p_record = {}
whiteblack = []
score = 0
black = 0
scorenum = 0
count = 0
temp = [0, 0, 0, 0, 0, 0]
NoOfWins = 0
winlose = ""
hiddencodeExample = ['B', 'P', 'G', 'Y']
player_guess = []
colors = ['B', 'R', 'Y', 'O', 'G', 'P']
color_code = random.sample(colors, 4)         
print ("HIDDEN CODE", hiddencodeExample)
num = int(input('Please enter number of players: '))
for i in range(num):
         names = str(input('Please enter a name: '))
         #p_record[] = names
         p_record = {names: {"Score":scorenum, winlose: NoOfWins}}
         #p_record["Score"].append(scorenum) 
         print(p_record)
         #p_record.append([names, scorenum, 0])
         # print(p_record[0])
         # print(scorenum)
         # print(p_record[1])
         # print(p_record)
         # print(names)
while game:  # for x in range(0, num)
         for player in list(p_record):
         #for player in p_record.keys():
                  print(player, ",", "make a guess of 4 colors from RGBYOP: ")          
                  player_input_guess = input("").upper()
                  player_guess = player_input_guess
                  # print(p_record[0], p_record[1])
                  # print(player_guess[0])
                  # score1 = ''.join(score)
                  # score.append(scorenum)
                  # print(score)
                  whiteblack.clear()
                  print("Current player:", player, "Current Score:", p_record.get(scorenum))
                  for i in range(len(player_guess)):  # BORY
                           # print(i)
                           if(player_guess[i] == hiddencodeExample[i]):  # iterate every character and check if tally
                                    # print("position no",i, player_guess[i])
                                    black += 1
                                    scorenum += 5
                                    whiteblack.append("B")
                                    # p_record.insert(1, scorenum)
                                    #player[1] += 5


                                    p_record.update({"Score": 5})
                                    # print(player[1])

                           elif player_guess[i] != hiddencodeExample[i] and player_guess[i] in hiddencodeExample:  # 
                                    # print("2position no", i, player_guess[i])
                                    white += 1 
                                    scorenum += 1
                                    whiteblack.append("W")
                                    #player[1] += 1
                                    #p_record["Score"] += 1
                                    p_record.update({"Score": 1})
                                    # print(player[1])
                                    # p_record.insert(1, scorenum)

                           else:
                                    whiteblack.append("")  

                  print(whiteblack)
                  print(p_record)
                  color_codeString = ''.join(hiddencodeExample)                    
                  print("Current player:", player, "Updated Score:", p_record.get(scorenum))
                  # whiteblack.reverse()
                  # print(whiteblack.reverse())  
                  whiteblackString = ''.join(whiteblack)

                  print("Result", whiteblackString)

                  if(player_guess == color_codeString):
                           game = False
                           print("Correct Guess!")
                           print("Winner: ", p_record)
                           print("<ENTER> to play and any letter to stop:  ")
                          # if keyboard.is_pressed('ENTER'):
                          #          game = True
                          # else:
                          #          game = False

main()

但是显示屏显示(两名玩家在玩)大卫和本:

David,请从RGBYOP中猜出4种颜色: //下一个播放器

David,请从RGBYOP中猜出4种颜色: //下一个播放器

得分,从RGBYOP中猜出4种颜色: //下一个播放器

David,请从RGBYOP中猜出4种颜色:

我想要的是让两名玩家玩:

David,请从RGBYOP中猜测4种颜色://下一个播放器

奔,从RGBYOP中猜出4种颜色://下一个播放器

2 个答案:

答案 0 :(得分:1)

我不知道其余的代码实现,但是似乎您正在遍历字典中的每个元素,相反,您只是想要它的条目。

for player in list(p_record)

应替换为

for player in p_record.entries()

也许是您的问题。

答案 1 :(得分:0)

您说的每个循环都将覆盖p_record对象的内容。

p_record = {names: {"Score":scorenum, winlose: NoOfWins}}

p_record因此是Ben或David。要解决此问题,您可能应该重组对象。尝试使用:

p_record[names] = {"Score":scorenum, winlose: NoOfWins}

并围绕它进行重组。 这会使p_record看起来像:

{'David': {'Score': 0, '': 0}, 'Ben': {'Score': 0, '': 0}}

例如,您可以使用p_record [“ David”] [“ Score”]访问David的分数

希望这会有所帮助!

注意::只需将“ p_record =“更改为“ p_record [names] =””,代码几乎就可以正常工作(例如:它会按需交替显示,但游戏的其余部分无法正常运行),但您应按照上面的建议进行清理,因为无需两次存储名称。

此外,您还应该尝试使用允许运行时对象检查和断点的IDE。弄清楚为什么它不能如此工作,要容易得多。