Dmitri_V
Dmitri_V

Reputation: 5

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']

Upvotes: 0

Views: 87

Answers (2)

quamrana
quamrana

Reputation: 39354

You should also be passing parameters into your function and returning a result:

def multiplyUp(userInput, factor):
    return [int(x) * factor for x in userInput]

in1 = input('Enter ').split()
in2 = 10

print(multiplyUp(in1, in2))

NB: code shamelessly stolen from @DirtyBit

Upvotes: 0

DirtyBit
DirtyBit

Reputation: 16772

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]

Upvotes: 2

Related Questions