PythonOnly执行first prin

2024-09-30 18:19:36 发布

您现在位置:Python中文网/ 问答频道 /正文

animals=["zebras", "dogs", "cats", "snakes", "insects"]

A=0
B=1
while B<= (len(animals)-1): 
    if animals[A] > animals[B]:
        animals= [animals[B]] + [animals[A]] + animals[B+1:]
        B= B + 1
        print(B)
        print(animals)

print("test- Will this not print as well?")

Okay thanks, that's seems to have worked. I do have one question still, however: how come it printed B but not the next print statement: print(animals)? When I comment out the print B statement then the print(animals) works.

this is the output I get when I run the code (as is) with the while loop not terminating.

output: 2


Tags: theoutputishaveasnotthisstatement
1条回答
网友
1楼 · 发布于 2024-09-30 18:19:36

它不会打印,因为你永远卡在while循环中

第一次运行时,它将检查

"zebras" > "dogs"

如果这是真的,它就会打印出来

2
['dogs', 'zebras', 'cats', 'snakes', 'insects']

再说一遍

"zebras" > "cats"

同样正确,指纹

3
['cats', 'dogs', 'snakes', 'insects']

然后就开始了

['insects', 'cats']

但当它现在试图检查动物[0]>;动物[3],它得到问题,返回False,因为动物[3]不存在。因此你的无限循环

相关问题 更多 >