Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

Notice Board

Per partecipare al corso di Fondamenti di programmazione 2023-24 loggatevi e attivatelo nella vostra pagina dei corsi preferiti. A quel punto il corso appare nel menù personale cliccando sul proprio avatar. Per i materiali degli anni precedenti seguite lo stesso metodo.

To join the Programming/Lab 2023-24 course, log-on and select it on the my courses page. It will appear on the personal menu of your avatar. For earlier years use the same method.

subtracting the previously used letters to num_letters

m
mjijena (280 points)
1 3 7
in HW2 required by (280 points)
I created a function which tells me how many letters a word contains and I assigned the variable " i " to " num_letters "

My problem is that now I want to subtract the number of letters of each word, obtained from my function, from " i " but it doesn't let me since " i " is an integer and I'm supposing the result from the function which tells me the letters in every word of a string is not an integer (I don't really know what it is)

I also tried using map, such as: i = i - map(function_of_letters, g1[0]) but when I run it it tells me " unsupported operand type(s) for -: 'int' and 'map' "

How could I convert the number of letters of each word into an integer so I can make the operation?
263 views

1 Answer

S
Silktrader (2550 points)
2 6 16
by (2.6k points)
edited by

... when I run it it tells me " unsupported operand type(s) for -: 'int' and 'map' "

`Map` returns its own object, an iterator — not a sequence, not a single value. The interpreter justifiably complains it can't subtract an iterator from an integer.

`Map` won't apply the function you specified until it is actually iterated. One way to do so would be to invoke the list constructor, such as:

list(map(custom_function, custom_sequence))

You'd be getting a new sequence though, not a single numeric value as the integer subtraction requires.


I'm supposing the result from the function which tells me the letters in every word of a string is not an integer (I don't really know what it is)

You should investigate what type you're getting so to understand the error. The IDE's hints, debugging or printing the type might help:

print(type(your_function(argument)))

You probably need to rewrite the function that tells you how many letters a word contains.

I am going to point out that a word, or string, is an immutable, ordered, sequence of characters ... so it has a length. You could perhaps count letters in a word with the built-in `len` function.