mercredi 30 septembre 2020

how to handle error not enough values to unpack in for loop

In the following code user will enter single integer N as number of pairs that he should enter next as b. Assume the entered list is b=[('r', 1),('a', 1),('a', 2),('a', 1),('r', 1),('r', 2)] and the output should be B=[1].User will input data like below:

6
r 1
a 1
a 2
a 1
r 1
r 2

The code raised with error: 'not enough values to unpack (expected 2, got 1)'

N = int(input())
B=[]
for i in range(N):
    b = (input().split())
for (action, value) in b:
  if action == 'a':
    B.append(value)
  elif action == 'r':
    try:
      B.remove(value)
    except ValueError:
      pass

Aucun commentaire:

Enregistrer un commentaire