I'd like to get multiple numbers from input in python3, but the following two things don't work well. Why?
Example 1
s=input().split()#s_1s_2 divided and put a value in s
print(s)# Output: ['s_1', 's_2' ]
print(s[0])# Output: s_1
print(s[1])# Output: s_2
print(s[2])# Output: s_3
Example 2
a,b,c=map(int,input().split())
print(a+b+c)
Output: I would like to output it as shown below.
587
20
Please let me know if there is any other good way.Thank you for your cooperation.
python python3
If you want to receive a fixed length input like, "You will definitely be given three inputs, so I want to receive those three," then the program in Example 2 should work.
a,b,c=map(int,input().split())
print(a+b+c)
Otherwise, if you want to receive variable length input like "I don't know how many, but I want to receive them", it's convenient to receive them as a list similar to Example 1.
number=list(map(int, input().split()))
print(number[0]+number[1]+number[2])# (if you know there are always 3 entries)
If you receive it like this, you can enter the first number[0]
, the second number[1]
, the third number[2]
, and so on.You can use len(number)
to find out how many inputs there were.
By the way, how do the programs above receive input?Understanding this makes it easier to understand the cause of the error, so I'll explain it to you.
a,b,c=map(...)
to get three integer values from the beginning of the iterator and substitute a
, b
, and c
, respectively.Each variable contains one integer value.numbers[0]
can be added to a list instead of an iterator. list()
can be used to convert iterators into lists. numbers=list(map(...)
contains integers.
Let's take a look at the specific examples.If the input is 587
, each function will process as follows:
↓input()
"587" (string)
↓ split()
["5", "8", "7"] (list of strings)
↓ map(int,...)
an iterator that returns [5, 8, 7]
↓ list()
5, 8, 7 (list of integer values)
If you specify the string you entered and the error message as in the comments in the multi-post destination, you will get a better answer.
If If you search the page for "Unpack with asterisks*" at the link above, you can code two array elements without errors as shown below. However, arguments with asterisks expand as arrays with or without values, so you must convert them to the int type. By the way, you can only use one asterisk.(If you use multiple asterisks, you will not be able to determine which variable to put the abbreviated value in.) SyntaxError: invalid syntaxa,b,c=map(int,input().split())
is a problem with alueError: need more than 1 value to unpack
when the input is two, it may be resolved only after Asterisk*.s="58"#input instead of repeatable string assignment
a, b, *c = map(int, s.split())
print(a,b,c)
# 5 8 []
s="587"#input instead of repeatable string assignment
a, b, *c = map(int, s.split())
print(a,b,c)
# 58[7]#c becomes an array
s = "58"
a, b, *c = map(int, s.split())
c=c[0] ifcelse0# else is the default value when omitted.
print(a,b,c)
# 5 8 0
Therefore, the following code with asterisks for variables b
and c
is an error, so if you want to allow indefinite array elements, consider another solution. a, *b, *c=map(int, s.split())
© 2025 OneMinuteCode. All rights reserved.