[英]How to take multiple inputs of different data types on same line in python 2? can't use map() function as input has mixed data
For ex: i want to take input as A 50 60 70. A is student name and next three numbers are marks of A in 3 subjects.例如:我想输入 A 50 60 70。A 是学生姓名,接下来的三个数字是 3 个科目的 A 分数。 Help me in storing data as above.
帮助我存储上述数据。 I have to collect that data for n students.
我必须为 n 个学生收集这些数据。
n=input()
for i in range(0,n):
St=raw_input(), Eng,Mat,Sci=map(int,raw_input().split())
print st
print Eng,Mat,Sci
Error:错误:
sh-4.3$ python main.py
File "main.py", line 3
St=raw_input(), Eng,Mat,Sci=map(int,raw_input().split())
SyntaxError: can't assign to function call
You can keep multiple lines of code in one line by separating them with a ;
您可以将多行代码保留在一行中,方法是用
;
分隔它们;
, as long as they're in the same indent level. ,只要它们处于相同的缩进级别。 And since it's python 2, shouldn't
n=int(raw_input())
?而且因为它是 python 2,不应该
n=int(raw_input())
吗?
n=int(raw_input())
for i in range(0,n):
St=raw_input(); Eng,Mat,Sci=map(int,raw_input().split())
print st
print Eng,Mat,Sci
But...this isn't a good practice, as it makes your code harder to read.但是……这不是一个好习惯,因为它会使您的代码更难阅读。 You should separate them on different lines.
您应该将它们分开在不同的行上。
n=int(raw_input())
for i in range(0,n):
St=raw_input()
Eng,Mat,Sci=map(int,raw_input().split())
print st
print Eng,Mat,Sci
In Python, split() function works for taking multiple inputs as a list data type.在 Python 中, split() 函数用于将多个输入作为列表数据类型。 If you need int,char or string type data then you need to apply typecasting of Python.In the solution,I am using the same approach.
如果您需要 int、char 或 string 类型的数据,那么您需要应用 Python 的类型转换。在解决方案中,我使用相同的方法。
n=int(raw_input())
for i in range(0,n):
s=str(raw_input())
St,Eng,Mat,Sci=raw_input().split()
Eng=int(Eng)
Mat=int(Mat)
Sci=int(Sci)
print St,s
print Eng,Mat,Sci
I think this would do the job in python 3.我认为这可以在 python 3 中完成这项工作。
n = int(input())
marks = {}
for i in range(n):
name, *line = input().split()
scores = list(map(float, line))
marks[name] = scores
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.