kflaw
kflaw

Reputation: 424

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)

Upvotes: 1

Views: 215

Answers (2)

Anthony Kong
Anthony Kong

Reputation: 40664

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:

enter image description here

Upvotes: 1

Nwaiwu Gilbert
Nwaiwu Gilbert

Reputation: 163

There is nothing wrong with the code. I just copied it and ran through the IDLE. Output is exactly as you specified

Upvotes: 1

Related Questions