+ 1

Python

a,b=int(input()).split(',') print(a) print(b) I did this code to separate two inputs inputs its showing error why?

15th May 2024, 4:17 PM
Raine04
Raine04 - avatar
12 Answers
+ 9
Raine04, Ausgrindtube , a, b = int(input()).split(',') > the code shown from the op fails, because it tries to split an integer coming from input() and int() conversion. a, b = input().split(',') > split() does not necessarily create a list. if we modify the code by removing the int() conversion, the code will work if exactly 2 input tokens are given. a = input().split(',') > so if we have to handle an unknown number of tokens, it is better to give only 1 variable that will be a list. if we need to get the input as numerical values (input always return string values by default), they have to be coverted to the desired data format.
15th May 2024, 8:06 PM
Lothar
Lothar - avatar
+ 7
as already mentioned, we can convert the input strings to numerical values. this can be done like: a = list(map(int, input().split(','))) or we can use a list comprehension like: c = [int(num) for num in input().split(',')] both version result in a list of integer numbers.
15th May 2024, 8:26 PM
Lothar
Lothar - avatar
+ 5
In Python, you should split the input by calling the `split()` method on the input string directly, not on the result of `int(input())`.
16th May 2024, 2:46 PM
Zaim Aalam
Zaim Aalam - avatar
+ 5
Per Bratthammar thats cool!
16th May 2024, 6:42 PM
Oma Falk
Oma Falk - avatar
+ 4
Lets try to unpack this no pun intended.  a, b = input() print(a) print(b) This code unpacks each character of the input string into a and b. For input 12, the output is: 1 2 Now consider: a, b = int(input()) This code tries to: 1. Get a string from the user. 2. Convert it to a single integer. 3. Unpack this integer into a and b. This is not possible because int( returns a single integer, resulting in a TypeError. This code just wrong: a, b = int(input()).split(",") Simply put, split(",") can't be called on an integer this way. Correct approach: a, b = input().split(',')  a = int(a) b = int(b)  print(a) print(b) 1.creates a list. [“a”,”b”] 2. Unpack into a and b. 3.Convert a to int 4.convert b to int Running out of space if you need other examples explained let me know. I’ll try my best!
16th May 2024, 3:57 AM
Chris Coder
Chris Coder - avatar
+ 4
In Python, unpacking can be done directly from an iterator, like this generator: >>> a, b = (int(x) for x in input().split(",")) or this map-object: >>> a, b = map(int, input().split(','))
16th May 2024, 5:59 PM
Per Bratthammar
Per Bratthammar - avatar
+ 3
The split method takes an input and creates a list, so the a,b wouldn't work. Read here: https://www.w3schools.com/JUMP_LINK__&&__python__&&__JUMP_LINK/ref_string_split.asp
15th May 2024, 4:24 PM
Ausgrindtube
Ausgrindtube - avatar
+ 3
a,b = tuple(int(x) for x in input().split(","))
16th May 2024, 6:52 AM
Oma Falk
Oma Falk - avatar
+ 3
a,b = input().split(',') print(int(a)) print(int(b))
16th May 2024, 4:49 PM
Shaxzodbek Allaberganov
Shaxzodbek Allaberganov - avatar
+ 1
You are trying to call the split() method on a number because you placed the input() function in the int() function the correct way would be: a, b = input().split(",") print(a) print(b)
15th May 2024, 8:06 PM
Buterbrod:)
Buterbrod:) - avatar