3 Values (numbers) In 1 Input Separation. Python 3
I'm working on a code right now that a part of it requires to ask the user for 3 different numbers in one line ( could be any number of digits in each number). Say I ask the user f
Solution 1:
Use a combination of split and sequence unpacking.
user_input = user_input(" Please enter the numbers: ")
a, b, c = user_input.split()
split
will take your string of numbers, say "x y z", and turn it into a list of elements in the string where the elements are all the words in the string that are separated by spaces. Thus split
will yield the string ['x', 'y', 'z'] for input 'x y z'.
Since a list is a form of sequence, its elements can be "unpacked" and assigned to a list of variables of your choosing.
Solution 2:
x = (input("Enter 3 user inputs: ").split())
a = int(x[0])
b = int(x[1])
c = int(x[2])
print(f"A: {a}, B: {b}, C: {c}")
Solution 3:
You can also do like this
a, b, c = [int(x) for x ininput("Please enter the numbers: ").split()]
Post a Comment for "3 Values (numbers) In 1 Input Separation. Python 3"