【问题标题】:How to stop printing items in a list when the counter is reached到达计数器时如何停止打印列表中的项目
【发布时间】:2021-04-06 17:32:09
【问题描述】:

我正在尝试运行一个循环,在列表中打印歌词行,然后在达到可变限制量时停止。虽然我已经尝试了两种不同结果的两种方法,但都不正确。

我遇到的问题:

lyrics = ["I wanna be your endgame", "I wanna be your first string",
          "I wanna be your A-Team", "I wanna be your endgame, endgame"]
lines_of_sanity = 6

#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.

#Recall the Earworm problem (3.3.5 Coding Exercise 2). The
#first time, you would still finish printing the entire list
#of lyrics after lines_of_sanity was exceeded.
#
#Revise that code so that you always stop when lines_of_sanity
#is reached. If lines_of_sanity is 6, you would print 6 lines,
#no matter how many lines are in the list. If there are fewer
#than 6 lines in the list, then you'd repeat the list until
#the number of lines is reached.
#
#For example, with the values above, you'd print:
#I wanna be your endgame
#I wanna be your first string
#I wanna be your A-Team
#I wanna be your endgame, endgame
#I wanna be your endgame
#I wanna be your first string
#MAKE IT STOP
#
#That's 6 lines: the entire list once, then the first two lines
#again to reach 6. As before, print MAKE IT STOP when you're
#done.
#
#HINT: There are multiple ways to do this: some involve a small
#change to our earlier answer, others involve a more wholesale
#rewrite. If you're stuck on one, try to think of a totally
#different way!


#Add your code here! Using the initial inputs from above, this
#should print 7 lines: all 4 lines of the list, then the first
#two lines again, then MAKE IT STOP

我的答案及其产生的结果:

counter = 0
while counter <= lines_of_sanity:
    for item in (lyrics):
        print(item)
        counter += len(lyrics)
print("MAKE IT STOP")

我们用歌词 = ["I want to be your endgame", "I want 成为你的第一根弦”、“我想成为你的 A-Team”、“我想成为你的 endgame, endgame"], lines_of_sanity = 6. 我们希望您的代码能够 打印这个:

我想成为你的残局我想成为你的第一根弦我想成为你的 A-Team 我想成为你的残局,残局我想成为你的残局我 想成为你的第一个字符串 MAKE IT STOP

但是,它打印了这个:

我想成为你的残局我想成为你的第一根弦我想成为你的 A-Team 我想成为你的残局,残局 MAKE IT STOP

在这一点上,它似乎只是比lines_of_sanity少打印一行

或者我的第一次尝试是:

counter = 0
while counter <= lines_of_sanity:
    for item in (lyrics):
        print(item)
        counter += 1
print("MAKE IT STOP")

这似乎将列表中的每个项目打印两次(直到超过 lines_of_sanity),这给了我 8 行

【问题讨论】:

    标签: python loops


    【解决方案1】:

    编辑:在进一步思考后简化了我的答案 - 这是一个用模运算符 % 解决的好问题。取模运算符的作用是返回第一个操作数除以第二个操作数后的余数。虽然不明显,但模运算符非常适合在列表中循环索引。

    例子:

    lyrics = ["I wanna be your endgame", "I wanna be your first string", 
              "I wanna be your A-Team", "I wanna be your endgame, endgame"]
    
    lines_of_sanity = 6
    
    for i in range(lines_of_sanity):
        print(lyrics[i % len(lyrics)])
            
    print("MAKE IT STOP")
    

    输出:

    I wanna be your endgame
    I wanna be your first string
    I wanna be your A-Team
    I wanna be your endgame, endgame
    I wanna be your endgame
    I wanna be your first string
    MAKE IT STOP
    

    【讨论】:

      【解决方案2】:
          lyrics = ["I wanna be your endgame", "I wanna be your first string",
                    "I wanna be your A-Team", "I wanna be your endgame, endgame"]
          lines_of_sanity = 6
          #divide our problem in 2 parts
          #In first part printing all strings of list 
          #if lines_of_sanity more than size of list.
          while lines_of_sanity > len(lyrics):
              for i in lyrics:
                  print(i)
              lines_of_sanity -= len(lyrics)
          #In second part printing remaining strings
          for i in range(lines_of_sanity):
              print(lyrics[i])
          
          print("MAKE IT STOP")
      

      【讨论】:

      • 感谢您的回答!虽然这适用于当前歌词,但当代码针对其他歌词进行测试时,例如:我们使用歌词 = [“我不能自己做这一切,不知道”,“我不是超人” ],lines_of_sanity = 12。我们希望您的代码打印以下内容:IndexError: list index out of range
      • 一切都适合我,也许有些东西与变量不匹配?
      【解决方案3】:
            lyrics = ["I wanna be your endgame", "I wanna be your first string",
            "I wanna be your A-Team", "I wanna be your endgame, endgame"]
            lines_of_sanity = 2
      
            `while lines_of_sanity > len(lyrics):
                 for i in lyrics:
                     print(i)
                 lines_of_sanity -= len(lyrics)
      
            for i in range(len(lyrics)):
               print(lyrics[i])
      
            print("MAKE IT STOP")
      

      `

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      【解决方案4】:

      一开始我想太多了,答案很简单

      count = 0
      while lines_of_sanity > count:
          for line in lyrics:
              count += 1
              if lines_of_sanity >= count:
                  print(line)
      print("MAKE IT STOP")
      

      您需要设置一个条件来阻止打印当前歌词的所有 4 行。所以我在打印任何行之前添加了一个 if 语句。

      【讨论】:

        猜你喜欢
        • 2023-02-02
        • 2017-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-16
        • 2018-07-06
        • 1970-01-01
        • 2017-11-22
        相关资源
        最近更新 更多