这是我的代码
students=["X" ,"Y" ,"Z" ]
age=[20,22,26]
def names():
global students
for students in students:
print "your name is"+" "+students+" "+"your age is"
names()
def ages():
global age
for age in age:
print age
ages()
想要输出
your name is X your age is 20
your name is Y your age is 22
your name is Z your age is 26
实际输出
your name is X your age is
your name is Y your age is
your name is Z your age is
20
22
26
如何逐个执行这些功能并获得所需的输出。
答案 0 :(得分:2)
试试这个
char
答案 1 :(得分:1)
>>> students=["X" ,"Y" ,"Z" ]
>>> age=[20,22,26]
>>> d = dict(zip(students, age))
>>> for k, v in d.items():
print 'your name is', k ,'your age is', v
答案 2 :(得分:0)
students=["X" ,"Y" ,"Z" ]
age=[20,22,26]
for i, name in enumerate(students):
print "your name is", name, "and your age is", age[i]
修改强>
必须承认,使用zip
作为另一个答案说更容易理解。
答案 3 :(得分:0)
你可以做的是迭代学生并使用枚举保持索引i。
for i, student in enumerate (students):
print "Your name is " + student +", "+ age[i]