简体   繁体   English

我哪里出错了 Sentinel 或破坏了这个 Python 代码

[英]Where am I going wrong with Sentinel or break in this Python code

Okay this program is almost done but it keeps running.好的,这个程序快完成了,但它一直在运行。 Need to figure out where the sentinel or break needs to go.需要弄清楚哨兵或休息需要去哪里。 This is what I have so far in Python.这是我目前在 Python 中所拥有的。 The program is suppose to read all the Pres on the list and print them.该程序假设读取列表中的所有 Pres 并打印它们。 Then there is a slice removing the first two and last two.Then it prints the list size is 6 and then proceeds to give me the 6 Pres in alpha reverse order.然后有一个切片删除前两个和最后两个。然后它打印列表大小为 6,然后继续以 alpha 反向顺序给我 6 个 Pres。 I have everything working other than its repeating the end indefinitely.除了无限期地重复结束之外,我所做的一切都在工作。 There has to be a while loop to display the elements in the list returned by playlist.必须有一个 while 循环来显示播放列表返回的列表中的元素。

Pres = ['Kennedy','Johnson','Nixon','Ford','Carter','Reagan',\
        'Bush','Clinton','Bush','Obama'] 
Pres2 = Pres[2:8]

def main():

    for names in Pres:

        print(names)


    PR3=playlist(Pres2)
    while playlist !='6':
            for PR3 in Pres2:
                    print(PR3)


def playlist(PR):

    size = len(PR)
    print('list size is now', size)

    PR.sort()
    PR.reverse()
    return PR

main()

Now this is what I should get when I run it.现在这是我运行它时应该得到的。

Kennedy
Johnson
Nixon
Ford
Carter
Reagan
Bush
Clinton
Bush
Obama
list size is now 6
Reagan
Nixon
Ford
Clinton
Carter
Bush

But instead after List size is now 6, the last 6 Presidents just keeps repeating.但是在列表大小现在是 6 之后,最后 6 位总统只是不断重复。 And it needs to read vertically on its own line.它需要在自己的行上垂直阅读。

It appears that you have some problems understanding even the basics of programming.看来您在理解编程的基础知识时也遇到了一些问题。 I strongly encourage you to read again the courses that you had already read.我强烈建议您再次阅读您已经阅读过的课程。

Nevertheless, here are some explanations:不过,这里有一些解释:

  • playlist is actually a function. playlist实际上是一个函数。 This while playlist !='6' loop is just verifying that this function object is not a string.这个while playlist !='6'循环只是验证这个函数对象不是字符串。 It is always different, so.它总是不同的,所以。 If you want to compare the result of the function, you have to call it: playlist(PR) , this will execute the function and return a list that you can store in a variable.如果你想比较函数的结果,你必须调用它: playlist(PR) ,这将执行函数并返回一个可以存储在变量中的列表。

  • Your playlist function is returning a list object.您的playlist函数正在返回一个list对象。 Why do you try to compare it with '6' ?为什么要尝试将它与'6'进行比较? Moreover, '6' is not the length of your list.此外, '6'不是您列表的长度。 It is a string.它是一个字符串。 6 is the length of your list as it is an int. 6是你的列表的长度,因为它是一个整数。

  • Why do you use a while loop since you only want the six Presidents to being displayed once?为什么要使用while循环,因为您只想显示六位总统一次? This does not make sense.这根本不符合逻辑。 A loop is for actions which need to be repeated an unknown number of times.循环用于需要重复未知次数的操作。

  • As Pres2 is sliced from Pres at the beginning, its length is 6. Its length has never been higher, and even assuming that your loop had been properly drafted, the code inside would never have been executed.由于Pres2是从Pres切出来的,所以它的长度是 6。它的长度从未如此之大,即使假设你的循环已经正确起草,里面的代码也永远不会被执行。

  • When you use for loop to iterate trough a list, the variable written after for is used as an alias which represents the current object iterated from your list.当您使用for循环遍历列表时,在for之后写入的变量用作别名,表示从您的列表迭代的当前对象。 for PR3 in Pres2: is weird as you already defined a variable called PR3 . for PR3 in Pres2:很奇怪,因为您已经定义了一个名为PR3的变量。 If you want to display president inside the list, then you just as to use for name in PR3 as you did before.如果你想在列表中显示总统,那么你就像以前一样for name in PR3使用for name in PR3

Let me show you an enhanced version of the function, hoping that you manage to understand better how it works:让我向您展示该功能的增强版本,希望您能够更好地理解它的工作原理:

def main():

    # Print the 10 Presidents
    for name in Pres:
        print(name)

    # Print the size of the list which already contains 6 Presidents
    # Then, sort the list, reverse it and return it
    # The new list is store into PR3
    PR3 = playlist(Pres2)

    # Print the 6 reverse sorted Presidents
    for name in PR3:
        print(name)

Did you get it?你明白了吗? You have absolutely no need to use a loop.您绝对不需要使用循环。

  1. Write a single program named mylist.py as follows.编写一个名为 mylist.py 的程序,如下所示。 Follow instructions carefully to avoid point deductions.仔细按照说明操作以避免扣分。

In the main function:在主函数中:

create a list that holds the surnames of recent USA presidents, starting with Kennedy and ending with Obama, in chronological order.创建一个包含最近美国总统姓氏的列表,以肯尼迪开头,以奥巴马结尾,按时间顺序排列。 use a for loop to iterate over the entire list, printing each president's name on its own line.使用 for 循环遍历整个列表,在自己的行上打印每个总统的名字。 make a slice by removing the first two presidents and the last two presidents from the list.通过从列表中删除前两位总统和最后两位总统来切片。 pass the new slice as an argument to a custom function named playlist.将新切片作为参数传递给名为 playlist 的自定义函数。 use a while loop to display the elements in the list returned by playlist.使用 while 循环显示播放列表返回的列表中的元素。 In the playlist function:在播放列表功能中:

print the size of the sliced list.打印切片列表的大小。 Use a list function.使用列表功能。 sort the sliced list in reverse alphabetical order.按逆字母顺序对切片列表进行排序。 return this list to main.将此列表返回到主列表。 Sample Output Original list in main: Kennedy Johnson Nixon Ford Carter Reagan Bush Clinton Bush Obama Not in main: list size is now 6 Back in main, list in reverse alpha order Reagan Nixon Ford Clinton Carter Bush示例输出 主要中的原始列表:Kennedy Johnson Nixon Ford Carter Reagan Bush Clinton Bush Obama 不在主要中:列表大小现在为 6 回到主要中,以反向 alpha 顺序列出 Reagan Nixon Ford Clinton Carter Bush

It calls for a while loop in the main function它在主函数中调用一个while循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM