-3

I'm trying to write a code to get whether an integer is in descending order. However, I can run this code only with the numbers like 123 or 321 but not with 34653. I always get this error when i use non-regular numbers : "IndexError: string index out of range". I have to use for-while loops to solve that question because we didn't cover the functions, lists etc. in the class. So, how can I fix it by using loops ? This is what I've done so far:

r_int = input("Enter integer value: ")
   
digits = len(r_int)

for i in range (0,digits+1):

    if r_int[i] > r_int[i+1]:
        print("Digits are in descending order")

        r_int = input("Enter integer value: ")
        
    elif int(r_int) <0:
        print("Value must be positive...")
        r_int = input("Enter integer value: ")

    else:
         print("Digits are not in descending order")

        
    
4
  • 2
    for i in range (0,digits) Commented Jul 8, 2020 at 13:24
  • @ParthShah That does not work. Commented Jul 8, 2020 at 13:25
  • Does this answer your question? Getting an IndexError: string index out of range Commented Jul 8, 2020 at 13:28
  • change the range of for loop to (0,digits-1) as you are comparing current and next element. Commented Jul 8, 2020 at 13:45

6 Answers 6

0

Just remove the plus 1's in the range function and in the first if statement.

Sign up to request clarification or add additional context in comments.

2 Comments

That just defeats the whole point of the program, to check if each digit is larger than the previous
Index's in arrays start at 0. So to get the first cell, you go to index 0, the second cell is at index 1, the third at index 2, and so on.
0

You will need to change it to:

for i in range (0,digits-1)

1 Comment

i don't get any output for the non-regular numbers when I use this.
0

Your error is happening because your argument for range() is digits+1.

The range(a, b) function iterates from a to b-1 which means that range(0, 5) returns [0, 1, 2, 3, 4] (although not a list with the latest versions of Python)

In your program since you are using the values i and i+1 you actually want to use digits-1 as the only argument for range(). See below:

r_int = input("Enter integer value: ")
   
digits = len(r_int)

for i in range (digits-1):

    if r_int[i] > r_int[i+1]:
        print("Digits are in descending order")

        r_int = input("Enter integer value: ")
        
    elif int(r_int) <0:
        print("Value must be positive...")
        r_int = input("Enter integer value: ")

    else:
         print("Digits are not in descending order")

1 Comment

I got this output when I ran this code: Digits are not in descending order Digits are not in descending order Digits are in descending order
0

after changing digits+1 to digits-1. you still have indexerror because you change the r_int value when for loop is running.

try this:

while True:
    r_int = input("Enter integer value: ")
       
    digits = len(r_int)
    check = 1
    for i in range (0,digits-1):
        if r_int[i] > r_int[i+1]:
            check = check + 1
        elif int(r_int) <0:
            print("Value must be positive...")
            r_int = input("Enter integer value: ")
    
    if check == digits:
        print("Digits are in descending order")
    else:
        print("Digits are not in descending order")

Output :

Enter integer value: 123
Digits are not in descending order
Enter integer value: 321
Digits are in descending order
Enter integer value: 345343
Digits are not in descending order
Enter integer value: -4
Value must be positive...

1 Comment

If it works, check it as the answer plz. Hit the check between the two arrows.
0

a simple approach can be this

num=input('enter your number here')
n=len(num)
flag=0
for i in range(0,n-1):
    if(int(num[i])-int(num[i+1])<0):
        flag=1
        break

if(flag==1):
    print('number not in decreasing order')
else:
    print('number in decreasing order')

this looks correct to me. check it and let me know.

1 Comment

great!! keep going.
0

Change the range of for loop, since r_int[i+1] becomes r_int[digits] in the last iteration which raises the error (remember strings are zero-indexed). This will work:

for i in range (0,digits-1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.