Reputation: 33
I'm having some issue how to actually calling the next player from LIST by sequence.
Basically, I need to check from p_record LIST that contains the player names.
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()
Here is the outcome that I wanted.
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]
Upvotes: 1
Views: 760
Reputation: 55479
You just need to loop over your p_record
list. Here's a simple example.
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
output
['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]
You may also find the examples here helpful: Asking the user for input until they give a valid response.
Upvotes: 1