【发布时间】:2021-07-26 16:36:09
【问题描述】:
我在 Python 中使用 for 循环时遇到了麻烦。我写了这段代码:
x=5
people=['Mary','Joe']
genders=['she','he']
for person in people:
print(person)
for gender in genders:
if x > 0:
print("{} is happy".format(gender))
输出是:
Mary
she is happy
he is happy
Joe
she is happy
he is happy
但我希望输出是:
Mary
she is happy
Joe
he is happy
有没有办法让 for 循环首先分别读取“Mary”和“she”,然后是“Joe”和“he”?
提前谢谢你。
【问题讨论】:
-
您可以使用 zip、下方或 enumerate。另外,您所说的“性别”实际上并不是性别。这是一个(人称)代词。
-
这是一个玩具密码...谢谢!