4
x1, y1, a1, b1, x2, y2 = int(input()), int(input()), int(input()), int(input()), int(input()), int(input())

我的问题是读取 6 个数字,每个数字都在新行中给出。如何比我上面的代码更简洁地做到这一点?

4

3 回答 3

8
x1, y1, a1, b1, x2, y2 = (int(input()) for _ in range(6))

在 Python 2 中替换为rangexrangeinputraw_input

于 2012-10-09T15:36:43.430 回答
1
x,y,z,w=map(int,input().split()) #add input in form 1 2 3 4


>>> x,y,z,w=map(int,input().split()) 
1 2 3 4
>>> x
1
>>> y
2
>>> w
4
>>> z
3
于 2012-10-09T15:38:14.980 回答
0

我会使用字典:

parm = {}
var_names = ['x1', 'y1', 'a1', 'b1', 'x2', 'y2']
for var_name in var_names:
    parm[var_name] = int(input())

那么您可以将字典键转换为变量,但我认为这不是一个好主意:

locals().update(parm)
于 2012-10-09T15:38:47.483 回答