While循环函数不会立即在JupyterLab中输出在另一个索引/while函数之后。

huangapple go评论62阅读模式
英文:

While loop function will not output on JupyterLab immediately following another index/while function

问题

List all the presidents whose names contain the letter 'o':

George Washington
John Adams
Thomas Jefferson
James Madison
James Monroe
John Quincy Adams
Andrew Jackson

List all the presidents whose names do not contain the letter 'w':
(Note: The provided code you shared has some formatting issues, and it's unclear why it's not working correctly.)

(Please note that the code you provided for this part of the assignment has formatting issues and might not be working as intended. It's difficult to determine the correct output without a properly formatted code.)

For each president, list the number of times the letter 'o' appears:

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

I'm new to python taking an academic college course with a lackluster professor. Please help.

Assignment states Using the while loop*

List all the presidents whose names contain the letter 'o'
List all the presidents whose names do not contain the letter 'w'
For each president, list the number of times the letter 'o' appears

My code:

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"]
## 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

Returns:
George Washington
John Adams
Thomas Jefferson
James Madison
James Monroe
John Quincy Adams
Andrew Jackson

##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

Upon attempting to run the function it immediately skips to the next cell and doesn't run. Same for the next function.

## 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

I tried moving the index outside of the if loop, and it only produces "George Washington" infinitely.

I previously used the same variable "US President" for both but I tried changing the second function to the variable "president" instead but it did not change anything.

I tried creating a second identical list with a different name and it also did not work.

I tried messaging the professor who gave me the following code which also didn't work:

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

答案1

得分: 1

你需要了解Python中的缩进是如何工作的。
例如,考虑用于打印"a" 5次的C/C++代码片段。

for(int i = 0; i < 5; i++){
    cout<<"a"<<endl;
}

for(int i = 0; i < 5; i++){
    cout<<"a"<<endl;
}

for(int i = 0; i < 5; i++) cout<<"a"<<endl;

它们都起到相同的作用。大括号定义了循环的作用域。

在Python中,缩进定义了每个循环/条件语句的作用域。
要打印"a" 5次,您需要编写:

i = 0
while i < 5:
    print("a")
    i += 1

这个片段运行print("a")i += 1语句,直到i变为5。另一方面,

i = 0
while i < 5:
    print("a")
i += 1

会无限打印"a",因为行i += 1从未在for循环内运行,而i < 5始终为真,因此会无限打印"a"。

让我们做一个小练习,查找数字列表中的偶数。这将为您解释整个概念。

Numbers = [4, 5, 8, 20, 24, 35, 54, 65, 37, 90]
countEven = 0  # 初始化计数器为0
# 以0为起始索引开始for循环
index = 0
while index < len(Numbers):
    # 下面的代码需要缩进一次,以便与循环一起运行
    if Numbers[index] % 2 == 0:  # 检查索引位置上的数字是否为偶数
        # 再次缩进一次,进入if语句的作用域
        countEven += 1
    # 下面的语句不应该在if语句的作用域内编写
    # 因为即使数字不是偶数,索引也需要增加
    # 如果在上面的if块内编写,它将永远卡在遇到的第一个奇数数字上
    # 因为然后if条件将始终为false
    # 并且索引变量永远不会增加
    index += 1

相同的原则需要应用于筛选包含"o"的名称和其他练习。

英文:

You need to understand how indentation in python works.
For example, consider the C/C++ code snippet to print "a" 5 times.

for(int i = 0; i &lt; 5; i++){
    cout&lt;&lt;&quot;a&quot;&lt;&lt;endl;
}

OR

for(int i = 0; i &lt; 5; i++){
    cout&lt;&lt;&quot;a&quot;&lt;&lt;endl;
}

OR

for(int i = 0; i &lt; 5; i++) cout&lt;&lt;&quot;a&quot;&lt;&lt;endl;

All of them work the same. The braces define the scope of the loop.

In python, the indentation defines the scope of each loop/conditional statement.
To print "a" 5 times, you need to write

i=0
for i &lt; 5:
    print(&quot;a&quot;)
    i+=1

This snippet runs the statement print(&quot;a&quot;) and i+=1 till i becomes 5.
On the other hand,

i=0
for i &lt; 5:
    print(&quot;a&quot;)
    i+=1

Infinitely prints "a" because the line i+=1 is never ran inside the for loop and i&lt;5 always remain true and thus printing "a" forever.

Let's do a small exercise of finding even numbers in a list of numbers. This will explain the whole concept to you.

Numbers = [4, 5, 8, 20, 24, 35, 54, 65, 37, 90]
countEven = 0 # initialize the counter to 0
#start the for loop with starting index to be 0
index = 0
for i &lt; len(Numbers):
    #The below code needs to be indented once so that it runs along with loop
    if Numbers[index] % 2 == 0 :#check if the number at index position is even
        #again indent once, to go within the scope of if statement
        countEven += 1
    #This below statement should be written NOT in scope of if statement
    #because index needs to be incremented even if the number is not even
    #if written inside the above if block then it will always be stuck at first
    #odd number it encounters because then if condition will always be false
    #and index variable will never increment
    index += 1

The same thing needs to be applied to filter out the names containing "o" and the other exercise.

huangapple
  • 本文由 发表于 2023年2月6日 03:49:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75355063.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定