-2

How can I input multiple values in one line? The output should look like a:8 b:5 c:6 d:4 e:0.1

I have tried this input:

a, b, c, d, e = int(input(' '.format(a:, b:, c:, d:, e:)))

but this didn't work.

bbnumber2
  • 643
  • 4
  • 18
NewInThis
  • 15
  • 1
  • 7

2 Answers2

1

This does what you want; the input numbers have to be entered with a comma in between:

a, b, c, d = (int(num) for num in input().split(','))

Explanation:

gotten_input = input('Enter a list of numbers separated by commas: ')
# user enters '1, 2, 3, 40, 500'
split_input_list = gotten_input.split(',')
# contains ['1', ' 2', ' 3', ' 40', ' 500']
numbers_tuple = (int(num) for num in split_input_list)
# contains (1, 2, 3, 40, 500)
# now the tuple's values are ready to assign
a, b, c, d, e = numbers_tuple
# now a=1, b=2, c=3, d=40, e=500

However if a floating point number is entered, int won't do what you want; if you want a mix of floats and ints, your logic will have to get a little more complicated:

a, b, c, d, e = (float(num) if '.' in num else int(num) for num in input().split(','))
    # uses the Ternary operator to determine if the numbers should be converted to float or int

To have the exact output format you asked for, you can format the string how the other answer had, other than without newlines:

print(f"a:{a} b:{b} c:{c} d:{d} e:{e}")

Or:

print("a:{} b:{} c:{} d:{} e:{}".format(*(int(num) for num in input().split(','))))
# The * unpacks the tuple into a list of arguments to send to format()
Random Davis
  • 6,662
  • 4
  • 14
  • 24
1

You can do like this:

a, b, c, d, e = input("Insert the 5 values: ").split()
print(f"a: {a}\nb: {b}\nc: {c}\nd: {d}\ne: {e}")

Input:

1 2 3 4 5

Output:

a: 1
b: 2
c: 3
d: 4
e: 5
User
  • 141
  • 1
  • 9