In your example you probably typed just one number on the line, and the error was raised because you expect 2 (a and b)
You should type both numbers on the same line, then enter.
You can change your code to accept any number of numbers, and store them in a list:
res = input("Enter your numbers:").split()
print(*res) ## prints the list content
for r in res: ## prints number one by one and one per line
print(r)
result:
Enter your numbers: 12 13 14
12 13 14
12
13
14
Answer from Malo on Stack Overflowpython - How can i use input().split() effectively to take at least 2 inputs from user in one line? - Stack Overflow
python - Using split on an input string - Stack Overflow
Split an input into two. Python 3 - Stack Overflow
How to split user input in python?
Can the input to the split() function in Python only be a string?
For the split() function in Python, what will the data type of the individual elements of the output array be?
Can the separator in Python's split() function be a number?
You might want to do exponentiation after converting to an int:
Use with AImylist = [int(i)**2 for i in list1]
You can't raise a string to a power (do you know what the square of the string "blah" is?), but you can raise a number to a power. So you need to convert the string to a number first.
Of course, a.split() returns a list of smaller strings derived from the original string, and you have to turn them into numbers yourself, but you already figured this out.
Error in your code: you are trying to raise power before converting it to int
Use with AIint(i**2)
#should be
int(i)**2
Another approach, using map function.
Syntax: map(func, iterable)
In python, String is iterable which means you can iterate over it. For example:
Use with AIfor i in "hello":
print(i)
#output will be:
h
e
l
l
o
Another simple concept before moving to map function: split() function will return the list.
Use with AIprint(type("hello".split()))
#output will be
<class 'list'>
Lets use map function to answer your question:
Use with AIa = map(int, input('Enter three digits separated by space:').split())
mylist = [i**2 for i in a]
print(mylist)
Explanation:
- The input() function will ask user to enter three numbers separated by space and that will be a string, now you can use split function over the string returned by input().
- Here, split function will return a list.
- Now, map function comes into the picture, it will take each element from the list returned at step 2 and map it to the function, in this case, it will map each element of the list to int.
- Variable a is map object, and map object is iterable, so you can apply for loop or list comprehension(you used list comprehension) to get desired output
You're calling the function wrongly.
>>> "hello world".split()
['hello', 'world']
split slits a string by a space by default, but you can change this behavior:
>>> "hello, world".split(',')
['hello', ' world']
In your case:
a,b= input("Please enter two digits separated by space").split()
The str.split() function is an attribute of the type str (string). To call it, you do:
input("Please enter two digits separated by space").split()
Note that .split(" ") isn't needed as that is what it is by default.