128k views
0 votes
Write a loop that subtracts 1 from each element in lowerscores. If the element was already 0 or negative, assign 0 to the element. For example, if lowerscores = 5, 0, 2, -3, the loop should transform it to 4, 0, 1, 0. What is the result of the loop?

1 Answer

2 votes

Final answer:

To subtract 1 from each element in lowerscores and assign 0 if the element was already 0 or negative, you can use a for loop.

Step-by-step explanation:

To subtract 1 from each element in lowerscores and assign 0 if the element was already 0 or negative, you can use a for loop. Here's one way to implement it in Python:

lowerscores = [5, 0, 2, -3]
for i in range(len(lowerscores)):
if lowerscores[i] > 0:
lowerscores[i] -= 1
else:
lowerscores[i] = 0

The loop iterates over each element in lowerscores. If the element is greater than 0, 1 is subtracted from it. Otherwise, 0 is assigned to the element. After the loop, lowerscores will be [4, 0, 1, 0], which is the result you're looking for.

User Haylem
by
8.5k points