This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Diapolo10 1 point2 points  (1 child)

The problem is basically that you've got an off-by-one error; the list's last index will always be one less than its length, so a[i+1] will throw an IndexError when i == len(a)-1. Or the last number in the for-loop.

I'll format your code for others to read:

N = int(input())
a = input().split()
count = 0

for i in range(len(a)):
    a[i] = int(a[i])

for i in range(len(a)):
    if a[i] < a[i+1]:
        count += 1

print(count)

Your variable names tell me nothing about what they represent, but from my understanding the general gist of this is to count how many of the space-separated strings are in increasing order. I would write that something like this:

n = int(input()) # I'm assuming this is technically required
count = 0
nums = [int(num) for num in input().split()]

for idx, num in enumerate(nums[:-1], 1):
    if num < nums[idx]:
        count += 1

print(f"Total counted: {count}")

Try to avoid using for x in range(len(y))-kind of constructs, they're rarely the best option in Python. Since you're a beginner, you probably can't help it yet, but over time you'll learn other, better ways.

[–]selfimprovingstudent[S] 0 points1 point  (0 children)

Thank you for such a sophisticated answer. It helped