[英]Printing Names using for loop
president = [('D. Trump',2017), ('Barack Obama',2009), ('B. Clinton',1993),
('George W. Bush',2001)]
max_w = president[0][1]
min_w = president[0][1]
for word in president:
if word[1] > max_w:
max_w = word
elif word[1] < min_w:
min_w = word
print(max_w, min_w)
why the max_w and min_w is a tuple?为什么 max_w 和 min_w 是一个元组? should it be a int??
应该是int吗??
I think you want max_w = word[1]
and min_w = word[1]
within those conditionals.我认为你想要在这些条件中
max_w = word[1]
和min_w = word[1]
。
Alternatively, you could write this:或者,你可以这样写:
for name, year in president:
if year > max_w:
max_w = year
if year < min_w:
min_w = year
Or save the initialization and do this (even better IMO):或者保存初始化并执行此操作(甚至更好的 IMO):
max_w = max(year for name, year in president)
min_w = min(year for name, year in president)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.