Yar Khan
Yar Khan

Reputation: 1

How to take multiple inputs from user using for loop?

for i in range(n - 1):
        nums = int(input('Enter numbers: '))

INPUT:

Enter numbers: 1
Enter numbers: 2
Enter numbers: 3
Enter numbers: 4

This is the output I am getting but I want the output to be in a single line, numbers that I am entering should be in a single line like 1 2 3 4. No commas no apostrophe

Please tell any solution

Upvotes: 0

Views: 1893

Answers (2)

user16836078
user16836078

Reputation:

You can append it first into a list

def myfunction(n):
    return [input('Enter numbers: ' for _ in range(n - 1)]

numbers = myfunction(5)

Enter numbers: 1
Enter numbers: 2
Enter numbers: 3
Enter numbers: 4

And then, you can use " ".join(numbers)

result = " ".join(numbers)
print(result)

1 2 3 4

If you do not even want to have the space:

result2 = "".join(numbers)
print(result2)

1234

Upvotes: 0

Mateo Vial
Mateo Vial

Reputation: 710

You can ask for a single input and then split it into words with the str.split() method

# Get the input
nums = input('Enter numbers :')

# Split and map to `int`
nums = nums.split()
nums = list(map(int, nums))

Upvotes: 1

Related Questions