How to remove whole strands?

Viewed 22

I have code that outputs only numbers, but it outputs them one at a time. Example. 222 is output as ['2.0', '2.0', '2.0'] also if the number is 2.32, output as ['2.0', '3.0', '2.0']. I need it to output them together, how do I do that? thanks for the help

x = input("text: ")
m = []

for i in x:
    if i.isdigit():
        m.append(float(i))
print(*m)
2 Answers
x = input("text: ").split(' ')
m = []

for i in x:
    try:
        m.append(float(i))
    except:
        pass

print(*m)

run :

>> text: 2.6 2.3 2.4 2.8
>>2.6 2.3 2.4 2.8

Separate the numbers with a space

I suggest you use a separator like , and split numbers accordingly:

x = input("text: ")
m = []

separator = ","

for i in x.split(separator):
    try:
        m.append(float(i.strip()))
    except ValueError:
        print("Invalid input", x)
        break
print(*m)

use as

>>> text: 32.43,222, 1e4
32.43 222.0 10000.0
Related