简体   繁体   English

while循环不会中断,当计数器不小于数组长度时应中断

[英]while loop won't break,should break when the counter is no longer less than the length of the array

trying to compare items in a and b and return greatest number at each index in list big - result should be [9,14,9,14,15,18,15].doing this for a class, must use while loop and counter 试图比较a和b中的项目并在列表big中的每个索引处返回最大数-结果应该为[9,14,9,14,15,18,15]。对一个类执行此操作,必须使用while循环和计数器

a = [7,12,9,14,15,18,12]
b = [9,14,8,3,15,17,15]
big = []
i = 0
length = len(a)
while i < length:
    if a[i] > b[i]:
        big.append(a[i])
    else:
        big.append(b[i])
    i = i + 1
print(big)

If you run your code directly in the python shell, you will get a SyntaxError. 如果直接在python shell中运行代码,则会收到SyntaxError。

For more info, see http://bugs.python.org/issue11433 有关更多信息,请参见http://bugs.python.org/issue11433

If you save the code down in a file, say test.py, then run python test.py , it will print out the result as expected. 如果将代码保存在一个文件中,例如test.py,然后运行python test.py ,它将按预期输出结果。

Edit: 编辑:

This answer is currently having a -1 rating. 该答案目前的评分为-1。 Before you downvote, can you actually read and try to understand the answer? 在您投票否决之前,您可以实际阅读并尝试理解答案吗?

This is what I am talking about: 这就是我在说的:

在此处输入图片说明

There is nothing wrong with the code. 代码没有错。 I just copied it and ran through the IDLE. 我只是复制了它并浏览了IDLE。 Output is exactly as you specified 输出与您指定的完全相同

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

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