0
Python>Pythonicness & Packaging>Tuple unpacking
numbers = (1, 2, 3) a, b, c = numbers print(a) print(b) print(c) How do I take the unpacking inputs? I tried this way: a, b, c = int(input("enter the numbers: ")) print(a) print(b) print(c) The error was: TypeError: cannot unpack non-iterable int object
2 Respuestas
+ 3
a, b, c = (int(i) for i in input().split())
Input example (separated by space):
1 2 3
+ 4
an other possibility would be:
a, b, c = map(int, input("enter the numbers: ").split())
Both versions will raise an exception if too less or to many values come from the input. (3 variables need exactly 3 separated input values). This can be covered with try: - except: block.