input function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

input function

#my code: x,y=int(input('enter two numbers: ').split(',')) #error: TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' explain please!

30th Dec 2020, 5:23 AM
Sahil Jain
Sahil Jain - avatar
2 Answers
+ 6
.split() converts and split string into a list. Example of split method: string = "This is a string" print(string.split()) >>> ["This", "is", "a", "string"] #It splits the string by whitespaces as default but you can split the string by whatever character you want - - - - - - - - - - - - - - - - - - - - - - - - And Here's what happened to your code: INPUT 1 4 x, y = int(input().split()) x, y = int(["1", "4"]) #and here this will cause an error cause you cant convert list into an integer. Perhaps what you are trying to do is to convert each element into integer. - - - - - - - - - - - - - - - - - - - - - - - - - - For this to work: x, y = [int(i) for i in input.split()] - - - - - - - - - - - - - - - - - - - - - - - - - - Then now.. x, y = [int(i) for i in ["1", "4"]] x, y = [1, 4] #x = 1 #y = 4 I hope this helps. If you need more explanation, feel free to ask. Thanks and Happy Coding!
30th Dec 2020, 5:25 AM
noteve
noteve - avatar
+ 4
You can also do this. x, y = map(int, input().split(","))
30th Dec 2020, 5:35 AM
Maninder $ingh
Maninder $ingh - avatar