简体   繁体   中英

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 = [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.

For more info, see 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.

Edit:

This answer is currently having a -1 rating. 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. Output is exactly as you specified

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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