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

9th Mar 2019, 4:38 PM
Pranav Gadre
Pranav Gadre - avatar
2 odpowiedzi
+ 3
a, b, c = (int(i) for i in input().split()) Input example (separated by space): 1 2 3
9th Mar 2019, 4:42 PM
Diego
Diego - avatar
+ 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.
9th Mar 2019, 6:31 PM
Lothar
Lothar - avatar