0

I'm doing some python challenges for fun and I've found a challenge which tells me to make a program that takes an input and prints the numbers in the message. but when I run the program it prints nothing but [] in the same number as the letters in the message, and also it do not recognize if a letter is actually a number or not, it just see every letter as a string and prints empty squares.

Here's the code:

WORDS = []
NUMBERS = []
Sentence = input()
for item in Sentence:
    if item == str():
        WORDS.append(item)
    if item == int():
        NUMBERS.append(item)

    print(('[%s]' % ', '.join(map(str, NUMBERS)))) 

Have any ideas?

user123
  • 69
  • 9
  • Possible duplicate of [What's the canonical way to check for type in Python?](https://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python) – Yuca Jul 24 '18 at 15:11
  • @Yuca nope, the op is actually working on strings only (read the whole code). – bruno desthuilliers Jul 24 '18 at 15:19
  • Ok, my rationale was that the explanation provided in that question is enough to answer this question. Do you have any suggestion as to what's the proper way to handle scenarios like this one? – Yuca Jul 24 '18 at 15:20

3 Answers3

1

Here is probably what you meant. You have to split the sentence first. All of the resulting items will be of type string, therefore isinstance will not help.

str.isdigit() checks if a string contains only digits. If it is a number, you can convert it to an integer using int.

WORDS = []
NUMBERS = []
Sentence = input()
for item in Sentence.split(): 
    if item.isdigit():
        NUMBERS.append(int(item))
    else:
        WORDS.append(item)

    print(('[%s]' % ', '.join(map(str, NUMBERS)))) 

If you do not do the split first, it will work too, but give you just single characters in the WORDS list and single numbers in the NUMBERS list.

phngs
  • 476
  • 4
  • 9
  • 1
    `instanceof` does not exist in Python, you want `isinstance()` – bruno desthuilliers Jul 24 '18 at 15:20
  • Hello, thank you but sadly it doesn't work as well :( Now i just get one square instant of how ever letters I've got in the message – user123 Jul 24 '18 at 15:23
  • 1
    If you also want to print the words (not the numbers) you have to add a statement for that, too. E.g. `print(WORDS)` – phngs Jul 24 '18 at 15:27
  • I fixed the problem guys, your suggests worked. my print was not in the right place and because of that i didn't saw the numbers. thank you for helping :D – user123 Jul 30 '18 at 06:29
1

Typechecking is usually done using isinstance(obj, cls) :

x = 42
print(isinstance(x, int))
print(isinstance(x, str))

but in your case this will not work since input() always returns a string (a string composed of numeric characters is still a string), so the proper solution is to check if the string is composed only of numeric characters (and eventually build an int from it if you need proper ints).

Also, input() returns a single string, and from your namings (WORDS) I assume you want to iterate on the distinct words, not on each characters like you actually do:

words = []
numbers = []
sentence = input()

for item in sentence.strip().split():
    if item.isnumeric():
        numbers.append(int(item))
    else:
        words.append(item)

print(('[%s]' % ', '.join(map(str, numbers)))) 
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
-1

Use the built-in isinstance function:

if isinstance(item, str):
    WORDS.append(item)
if isinstance(item, int):
    NUMBERS.append(item)
Green Cloak Guy
  • 23,793
  • 4
  • 33
  • 53
  • I've tried it and it doesn't work, still getting the same empty [] squares. any other suggestions? – user123 Jul 24 '18 at 15:17