You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JupyterLab中Python while循环无法正常执行问题求助

Python新手作业求助:while循环处理美国总统列表问题

我是大学Python课程的新手,教授教学水平欠佳,现在卡在作业上,作业要求必须使用while循环完成以下三个任务:

  • 列出所有名字包含字母'o'的美国总统
  • 列出所有名字不包含字母'w'的美国总统
  • 统计每位总统名字中字母'o'的出现次数

总统列表如下:

listUSPresidents=["George Washington","John Adams","Thomas Jefferson","James Madison","James Monroe","John Quincy Adams","Andrew Jackson","Martin Van Buren","William H. Harrison","John Tyler","James K. Polk","Zachary Taylor","Millard Fillmore","Franklin Pierce","James Buchanan","Abraham Lincoln","Andrew Johnson","Ulysses S. Grant","Rutherford B. Hayes","James A. Garfield","Chester A. Arthur","Grover Cleveland","Benjamin Harrison","Grover Cleveland","William McKinley","Theodore Roosevelt","William H. Taft","Woodrow Wilson","Warren G. Harding","Calvin Coolidge","Herbert Hoover","Franklin D. Roosevelt","Harry S. Truman","Dwight D. Eisenhower","John F. Kennedy","Lyndon B. Johnson","Richard M. Nixon","Gerald R. Ford","Jimmy Carter","Ronald Reagan","George H. W. Bush","Bill Clinton","George W. Bush","Barack Hussein Obama","Donald J. Trump"]

任务1:列出名字含'o'的总统

我的代码:

## List all the presidents whose names contain the letter 'o'
index = 0
while index < len(listUSPresidents):
    USPresident = listUSPresidents[index]
    if 'o' in USPresident.lower():
        print (USPresident)
        index +=1

问题:只返回部分结果,之后就停了。

任务2:列出名字不含'w'的总统

我的代码:

##List all the presidents whose names do not contain the letter 'w'
index = 0
while index < len(listUSPresidents):
    president = listUSPresidents[index]
    if ('w' not in president.lower()):
        print (president)
        index +=1

问题:JupyterLab直接跳转到下一个单元格,完全不执行循环。

任务3:统计每位总统名字中'o'的出现次数

我的代码:

## For each president, list the number of times the letter 'o' appears
index = 0
while index != len(listUSPresidents):
    USPresident = listUSPresidents[index]
    O_count = USPresident.lower().count('o')
    print (USPresident, " ", O_count)
    index +=1

问题:同样在JupyterLab里无法正常执行。

另外我尝试过把index自增移出if循环,结果导致无限输出"George Washington";换变量名、复制新列表都没用,教授给的代码也跑不起来:

#List all the presidents whose names do not contain the letter 'w'
counter = 0
while counter < len(listUSPresidents):
if listUSPresidents[counter].count("w")==0:
print(listUSPresidents[counter])
counter +=1

还有个问题:JupyterLab里执行完一个带索引的while循环后,后续的while循环没法立刻输出内容。


问题根源与修正方案

所有问题的核心都是索引自增的位置错误,以及教授代码的缩进问题:

任务1修正代码

# 列出所有名字包含字母'o'的美国总统
index = 0
while index < len(listUSPresidents):
    USPresident = listUSPresidents[index]
    if 'o' in USPresident.lower():
        print(USPresident)
    # 不管是否满足条件,索引必须每次都自增,否则会卡在不满足条件的元素上
    index += 1

你之前的代码只在满足条件时才让index加1,遇到名字不含'o'的总统时,index就停住了,循环一直卡在这个元素上,所以只输出了前面部分结果。

任务2修正代码

# 列出所有名字不包含字母'w'的美国总统
index = 0
while index < len(listUSPresidents):
    president = listUSPresidents[index]
    if 'w' not in president.lower():
        print(president)
    # 同样,索引必须每次循环都自增,不能只在满足条件时加
    index += 1

之前的代码遇到名字含'w'的总统时,index不增加,循环一直卡在第一个含'w'的元素(George Washington)上,进入死循环,JupyterLab就会卡住不输出。

任务3修正代码

# 统计每位总统名字中字母'o'的出现次数
index = 0
while index < len(listUSPresidents):
    USPresident = listUSPresidents[index]
    o_count = USPresident.lower().count('o')
    print(f"{USPresident}  {o_count}")
    index += 1

这个任务的代码逻辑其实没问题,但用index != len(...)不如index < len(...)稳妥,另外确保索引每次都自增(你这里是对的,可能之前JupyterLab的问题是因为前面的死循环导致内核卡住了)。

教授代码的问题

教授的代码缩进错误,Python是靠缩进区分代码块的,正确缩进应该是:

# 列出所有名字不包含字母'w'的美国总统
counter = 0
while counter < len(listUSPresidents):
    if listUSPresidents[counter].lower().count("w") == 0:  # 记得转小写,避免大小写问题
        print(listUSPresidents[counter])
    counter += 1  # 同样要把自增放在循环里,不管条件是否满足

JupyterLab卡住的解决办法

如果执行完死循环的代码后,后续代码无法运行,是因为Python内核卡住了,你可以点击JupyterLab工具栏里的"重启内核"按钮,然后重新运行代码即可。


内容的提问来源于stack exchange,提问作者OllieJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 16:55:17