0

this was an excrsize where I am supposed to make a list of random numbers and print the third smallest.

I do not understand why it wont print min_num.

import random
def the_third_smallest():
    random_nums = 0
    random_lst = []
    while random_nums < 20:
        random_lst.append(random.randint(1,100))
        random_nums += 1
    print (random_lst)
    min_num = random_lst[0]
    sivov = 0
    while True:
        for num in random_lst:
            if num < min_num:
                min_num = num
#         print(min_num)
        new_lst = []
        for mispar in random_lst:
            if mispar != min_num:
                new_lst.append(mispar)
#         print(new_lst)
        if new_lst == []:
            return
        if sivov == 2:
            return
        min_num = new_lst[0]
        random_lst = new_lst
        sivov += 1
    print(min_num)
  • `while True` runs indefinitely, that's why it never reaches `print(min_num)`. – Minh-Long Luu Mar 12 '23 at 16:05
  • 1
    Replace the `return`s with `break`s. That way you'll stop the loop but not bail out of the function, and so you'll get to the `print(min_num)` line at the end before you return. Alternatively: change the `return`s to `return min_num`, and then do `print(the_third_smallest())`. – Samwise Mar 12 '23 at 16:08
  • In addition to @Samwise The function `the_third_smallest` also need to be **called** to get the output. An unindented line `the_third_smallest()` would be needed at the end of the defined function. – Partha D. Mar 12 '23 at 16:10

2 Answers2

1

as others pointed out, break statements need to be used instead of returns. Have a look at this code.

import random

def the_third_smallest():
    random_nums = 0
    random_lst = []
    while random_nums < 20:
        random_lst.append(random.randint(1,100))
        random_nums += 1
    print (random_lst)
    min_num = random_lst[0]
    sivov = 0
    while True:
        for num in random_lst:
            if num < min_num:
                min_num = num

        new_lst = []
        for mispar in random_lst:
            if mispar != min_num:
                new_lst.append(mispar)

        if new_lst == []:
            break
        if sivov == 2:
            break
        min_num = new_lst[0]
        random_lst = new_lst
        sivov += 1
    return min_num

print(the_third_smallest())

Also, if you are interested in other approaches (not using numpy), you can look at this for your inspiration:

def the_third_smallest():
    # this is a short notation for a for-loop
    random_numbers = [random.randint(1, 100) for i in range(100)]
    # sorted() returns a generator that is turned to a list by list(). The third biggest element is the one at position two
    return list(sorted(random_numbers))[2]
Klops
  • 951
  • 6
  • 18
1

While your code is works, you need to break the loop instead of return. Breaking will stop the while loop wheras the return would end the function and return a value. Here is what the code would look like

from random import randint
def the_third_smallest():
  # making the list
  random_lst = [randint(1, 100) for _ in range(20)]
  # Finding the third smallest
  min_num = None
  sivov = 0
  while True:
    min_num = min(*random_lst) # Finding the smallest number
    # Making a new list without the min_num in it
    random_lst = [x for x in random_lst if x != min_num]
    sivov += 1
    if sivov == 3: break # Break on the third instance
  print(min_num) # Printing the third smallest number
  return min_num

While this was not asked for. Here is a better way of writting the above code

random_lst = [randint(1, 100) for _ in range(20)]
random_lst.sort()
print(random_lst[2]) # Third smallest number
Alpha Wolf Gamer
  • 320
  • 2
  • 12