Python 到达计数器时如何停止打印列表中的项目
我尝试运行一个循环,在列表中打印歌词行,然后在达到可变限制量时停止。虽然我用两种不同的方法尝试过,但都不正确 给我的问题是:Python 到达计数器时如何停止打印列表中的项目,python,loops,Python,Loops,我尝试运行一个循环,在列表中打印歌词行,然后在达到可变限制量时停止。虽然我用两种不同的方法尝试过,但都不正确 给我的问题是: 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_
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")
计数器=0
而计数器编辑:进一步思考后简化了我的答案-这是使用模运算符%
解决的一个好问题。模运算符的作用是在第一个操作数除以第二个操作数后返回余数。虽然不明显,但模运算符非常适合在列表上循环索引
例如:
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
谢谢你的回答!虽然这对当前的歌词有效,但当代码与其他歌词进行测试时,例如:我们用歌词=[“我不能自己做这一切,不,我知道”,“我不是超人”],第12行。我们希望您的代码打印以下内容:Indexer Ror:list index超出范围一切对我都有效,可能是变量不匹配?
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