How to get multiple inputs in one line in Python?
Here's what I want:
Python Input :
Enter name and age respectively: Subha 18
Output :
Your Name Is Subha and Your age is 18 years
Here is what I tried:
inp = input()
x = inp.split()
print x[0] # Won't work
print x[1]
The error is (console):
SyntaxError: unexpected EOF while parsing
Traceback (most recent call last):
File "source_file.py", line 3, in <module>
inp = input()
File "<string>", line 1
11 22
^
Use raw_input
:
inp = raw_input()
x = inp.split()
print x[0]
print x[1]
Python 3
There are multiple ways to get input in one line python statement, but that totally depends on data type choice.
For storing data in variable
x,y = map(data_type,input().split())
eg;
x,y = map(int,input().split())
For storing data in the list.
list_name = list(map(data_type,intput().split())
or
list_name = [data_type(input()) for i in range(n)]
for n could be range of input.
For storing input in the dictionary.
dict1= dict(zip(range(n),list(map(int,input().split()))) )
>>> init_input = input("Enter name and age respectively, eg 'Shubha 18': ")
Enter name and age respectively, eg 'Shubha 18': Marcus 21
>>> values = init_input.split(" ")
Returns a list: ['Marcus', '21']
The split() method returns a list of all the words in the string, taking the separator as an argument.
一次接受两个输入:
a, b = input("Enter a two value: ").split()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.