Jack Someone
Jack Someone

Reputation: 97

Store user input in list, have it available in %d format

I want to store a users input in different lists depending on the input. I'm doing this using

player_move_dif = [int(n) for n in input("Choose your move number %s (0 or 1):" % (turn+1))

Later on I want to reuse the input to display the input vs the computers "input" the following way

if player_move_dif == computer_move:
   MS = MS + 1
   print("player = %d machine = %d - Machine wins!" % (player_move_dif, computer_move))
   print("You: %d Computer: %d" % (PS, MS))
else:
   PS = PS + 1
   print("player = %d machine = %d - Player wins!" % (player_move_dif, computer_move))
   print("You: %d Computer: %d" % (PS, MS))

However this results in a TypeError: %d format: anumber is required, not list

what is a workaround for this to store the input in a list but still have it available as a number?

Upvotes: 0

Views: 50

Answers (2)

DirtyBit
DirtyBit

Reputation: 16772

player_move_dif is a list so you cannot compare it to a integer.

Change this:

if player_move_dif == computer_move:

to something like this:

for player_move in player_move_dif:
    if player_move == computer_move:
        MS = MS + 1
        print(f"player ={player_move} machine = {computer_move}- Machine wins!")
        print("You: %d Computer: %d" % (PS, MS))
    else:
       PS = PS + 1
       print(f"player = {player_move} machine = {computer_move} - Player wins!")
       print("You: %d Computer: %d" % (PS, MS))

Upvotes: 1

Lorenzo Paolin
Lorenzo Paolin

Reputation: 289

The problem is that you are creating player_move_dif as a list and then trying to access it as an integer. Since there's so little code the first suggestion that come in my mind is to use for loop to iterate over the elements in the list and than print them using %d

Upvotes: 0

Related Questions