0

I want to do something pretty simple, but I don not know why it is not working

N = int(input())
numbers = list(input())
print(numbers)
def switch(x):
    return {
      '1': 2,
      '2': 5,
      '3': 5,
      '4': 4,
      '5': 5,
      '6': 6,
      '7': 3,
      '8': 7,
      '9': 6,
    }[x]

print(list(map(switch, numbers)))

I have the above code and what I want is to based in a number sum all of the results for each of its digits from a function called switch

lucasbbs
  • 349
  • 4
  • 14
  • `numbers = list(input())` this will always be a list of one, i am guessing you want to put in multiple numbers like `1 2 3 6 5` if so then you need to split the string like `numbers = list(input("num: ").split())` – Chris Doyle Mar 30 '21 at 22:15
  • No @ChrisDoyle, the numbers are as such 115380, for instance I want to get [1,1,5,3,8,0] – lucasbbs Mar 30 '21 at 22:18
  • "I don not know why it is not working" We can only help diagnose the problem if you explain it. What happened when you tried the code? How is that different from what you want to happen? – Karl Knechtel Mar 30 '21 at 22:41
  • What should the result from `switch` be when the digit is `'0'`? It seems like you just have an oversight there, and then the *actual question* is "how do I sum a list of numbers in python?" which you answer by putting that exact thing [into a search engine](https://duckduckgo.com/?q=how+do+I+sum+a+list+of+numbers+in+python%3F). – Karl Knechtel Mar 30 '21 at 22:43
  • Either way, the question should be closed - as a typo, or as a duplicate of [Sum a list of numbers in python](https://stackoverflow.com/questions/4362586/sum-a-list-of-numbers-in-python). – Karl Knechtel Mar 30 '21 at 22:45
  • There is no such case of 0, @KarlKnechtel – lucasbbs Mar 30 '21 at 22:46
  • No, it should not, @KarlKnechtel – lucasbbs Mar 30 '21 at 22:47
  • " the numbers are as such 115380, for instance I want to get [1,1,5,3,8,0]" Clearly it is possible to have a `0` as one of the digits that you feed to the `switch`. – Karl Knechtel Mar 30 '21 at 22:47
  • "I am getting this error: Traceback (most recent call last): File "main.py", line 18, in print(sum(map(switch, numbers))) File "main.py", line 5, in switch return { KeyError: '0'" **That is because there is, in fact "such case"**. It is telling you exactly what the problem is (and which you should have said in the question originally). – Karl Knechtel Mar 30 '21 at 22:48
  • @KarlKnechtel, I just figured out what I should do... I need to filter out the zeros from my answer – lucasbbs Mar 30 '21 at 22:51

1 Answers1

1

Do you want to sum all digits? in that case you can use sum function:

...
print(sum(map(switch, numbers)))

You don't need to convert map result to list since sum accepts iterables.

Hook
  • 355
  • 1
  • 7
  • Hello, Hook, I am just having a problem when trying to get a list from the dictionary function in this line of code – lucasbbs Mar 30 '21 at 22:16
  • Why? it worked for me (i've tested it). can you copy your traceback? Also, make sure you don't need '0' because you started from '1'. – Hook Mar 30 '21 at 22:21
  • Works for me as well. However, since sum takes an iterator, there is no need for the list call, right? – Malcolm Mar 30 '21 at 22:22
  • @Malcolm Correct. – Hook Mar 30 '21 at 22:23
  • I am getting this error: Traceback (most recent call last): File "main.py", line 18, in print(sum(map(switch, numbers))) File "main.py", line 5, in switch return { KeyError: '0'  – lucasbbs Mar 30 '21 at 22:34
  • That is because you should add `'0': 0` to dictionary in `switch` function. – Hook Mar 30 '21 at 23:11