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? should it be a int??
I think you want max_w = word[1]
and min_w = word[1]
within those conditionals.
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):
max_w = max(year for name, year in president)
min_w = min(year for name, year in president)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.