Search code examples
pythonloopsuser-input

Multiple data in one user input


I am having a trouble with multiple numbers in one user input. The idea is to multiply each user`s number with a predetermined one. Does it even possible to loop through?

in1 = input('Enter ')
in2 = 10

def count():
    s = [x*in2 for x in in1]
    print(s)

count()

With input like this '5 5 5' terminal gave me this:

['5555555555', '          ', '5555555555', '          ', '5555555555']

Solution

  • Since the input() treats it as a str, using split() to have them space-separated, Convert each str to int and then multiply it by in2. Also, avoid using keywords as function names:

    in1 = input('Enter ').split()              # ['5', '5', '5']
    in2 = 10
    
    def func():
        return [int(x)*in2 for x in in1 if x]
    
    print(func())
    

    OUTPUT:

    Enter 5 5 5
    [50, 50, 50]