Multiple int input in same line python
You messed up with parenthesis (split works on string not int)
then you expect a tuple for x,y = ...
the solution is:
x, y = [int(i) for i in input("->").split()]
print(x, y)
How to input 2 integers in one line in Python?
Split the entered text on whitespace:
a, b = map(int, input().split())
Demo:
>>> a, b = map(int, input().split())
3 5
>>> a
3
>>> b
5
How to input two integers in a same line?
In Python 3, the input()
method will always return a string. Your provided code snippet tries to split that input, and the split()
function defaults to splitting on a space character.
The map()
function takes a function (in this case, the int
function), and applies that function to each part of the enumerable returned by the split()
function.
So, if you were to write a, b = map(int, input().split(" "))
, and then the user entered 123 456
, you would have a == 123
and b == 456
.
Let's take this as an example: a, b = map(int, input().split())
This is what's going to happen:
- The
input()
function gets executed, and let's say that the user enters123 456
- This gets interpreted as
"123 456"
, and then gets passed into the split function, like this:"123 456".split()
which returns a list that looks like this:["123", "456"]
. - Now you can sort of imagine that the code looks like this:
map(int, ["123", "456"])
, which might be a bit easier to reason about. - What's going to happen now is that the
map
function will take its first argument (theint
function), and apply that to each element of the["123", 456"]
list (that is,"123"
and"456"
). - The
map()
function returns an enumerable, which you in this case can think of as looking like the result of[int("123"), int("456")]
, which results in[123, 456]
- The unpacking of the assignment happens. You can think of that like this:
a, b = [123, 456]
How can I input multiple values in one line?
You can do like this:
a, b, c, d, e = input("Insert the 5 values: ").split()
print(f"a: {a}\nb: {b}\nc: {c}\nd: {d}\ne: {e}")
Input:
1 2 3 4 5
Output:
a: 1
b: 2
c: 3
d: 4
e: 5
Unable to input string values when taking multiple input in one line (Python)
You need to replace the +
with a ,
since the +
is going to concatenate your inputs into one string.
city, pet = input("What is the name of the city you grew up in?\t"), input ("\nWhat is the name of your first pet?\t")
Whenever you get the too many values to unpack
Error, make sure that the number of variables on the left side matches the number of values on the right side.
Related Topics
Pandas: Drop Consecutive Duplicates
Why Python 3.6.1 Throws Attributeerror: Module 'Enum' Has No Attribute 'Intflag'
Is It Pythonic: Naming Lambdas
Pandas Read_Csv: Low_Memory and Dtype Options
Python List VS. Array - When to Use
How to Create a Guid/Uuid in Python
Replace Console Output in Python
Using Os.Walk() to Recursively Traverse Directories in Python
"Python" Not Recognized as a Command
Pandas Groupby.Apply Method Duplicates First Group
Using Lambda Expression to Connect Slots in Pyqt
Saving a Numpy Array as an Image