0

The program I wrote by using 'for loop' is correct and passed all the testcases,however, when I transfer 'for loop' to 'while loop',an 'IndexError' occurred,I wonder what are the problems in my program which using 'while loop'.

nums = []
remainders=[]
while True:
    num=int(input('Number: '))
    if num==0:        
        break
    nums.append(num)    
if len(nums)==0:
    print('No integers were entered')
    exit()
a=0
while a<len(nums):
    num=nums[a]
    remainder=num%10
    remainders.append(remainder)
    a+=1
index=remainders.index(max(remainders))
count=0   
for i in remainders:
    if i > remainder:
        remainder=i
        count=0
    if i ==remainder:
        count+=1
if count ==1:
    print('The most interesting integer was: {}'.format(nums[index]))  
elif count>1 and count<len(nums):
    print('Two or more integers are the most interesting')
else:
    print('All integers have the same remainder')  

That's the program I wrote by using for loop, this answer is correct

nums = []
remainders=[]
while True:
    num=int(input('Number: '))
    if num==0:        
        break
    nums.append(num)    
if len(nums)==0:
    print('No integers were entered')
    exit()
a=0
while a<len(nums):
    num=nums[a]
    remainder=num%10
    remainders.append(remainder)
    a+=1
index=remainders.index(max(remainders))
count=0   
k=0
while k<len(remainders):
    remainder=remainders[k]
    if remainders[k]>remainder:
        remainder=remainders[k]
        count=0
    k+=1    
    if remainders[k]==remainder:
        count+=1
    k+=1    
if count ==1:
    print('The most interesting integer was: {}'.format(nums[index]))  
elif count>1 and count<len(nums):
    print('Two or more integers are the most interesting')
else:
    print('All integers have the same remainder')  

This answer is incorrect because the second while loop have some problems

actual:
Number: 4
Number: 20
Number: 9
Number: 3
Number: 5
Number: 0
Traceback (most recent call last):
  File "numbers.py", line 26, in <module>
    if remainders[k]==remainder:
IndexError: list index out of range
expected:
Number: 4
Number: 20
Number: 9
Number: 3
Number: 5
Number: 0
The most interesting integer was: 9
9
  • I think you need to re-check your question title. Commented May 5, 2019 at 6:23
  • How do you decide the most interesting integer ? Commented May 5, 2019 at 6:26
  • I just have a question about what are the problems in the second 'while loop' of my second program,what are the problems of the conversion Commented May 5, 2019 at 6:28
  • the most intereting integer is the integer that has the largest remainder after divided by 10 Commented May 5, 2019 at 6:29
  • Well the logic you are trying to do here also matters @Eric Please update the question with it Commented May 5, 2019 at 6:29

1 Answer 1

1

Two issues in your code

  • You are doing k+=1 twice, which is causing the list index out of range error, do it only once

  • When you do remainder=remainders[k] if remainders[k]>remainder, this will always evaluate to False, remove the line remainder=remainders[k]

Keeping this in mind, the following while loop should work

while k<len(remainders):
    if remainders[k]>remainder:
        remainder=remainders[k]
        count=0
    if remainder==remainders[k]:
        count+=1
    #Increment only once
    k+=1
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, however, when I change my incorrect while loop to this one,it just pass 8 of 12 testcases,when I changed the 'r' to remainder, all the testcases passed
what test cases are you talking about? I just ensured that the code runs for the input you provided!
Also you have another remainder somewhere in your previous while a<len(nums): and being used in if r>remainder:which is coming up here, try to isolate those two variables! Maybe you can also refactor your code to better isolate the issues!
Yes,the code runs for the input I provided, however, if there are more than one number have the largest remainder after divided by 10, the expected output should be 'Two or more integers are the most interesting'
Updated the loop! Now it should work for all cases @Eric

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.