Python Forum
How to detect and tell user that no matches were found in a list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to detect and tell user that no matches were found in a list (/thread-11584.html)



How to detect and tell user that no matches were found in a list - RedSkeleton007 - Jul-17-2018

It's one thing to tell someone we have reached the end of a list, but how do we tell the user if no matches were found?

#!/usr/bin/env python3
#PracticePythonExercise03.py

myList = [0,2,5,4,6,7,9,8]

userNumber = int(input("Pick a number: "))

print("the numbers in myList that are smaller than the number you entered are:")

for i in myList:
    if i < userNumber:
        print("Found a number less than " + userNumber + " which is: " + i)
    #If there are no numbers in myLsit smaller than userNumber, then tell the user
    if i == len(myList) and no i < userNumber has not been found...



RE: How to detect and tell user that no matches were found in a list - micseydel - Jul-17-2018

Write a function that returns true if you find it, and returns false after the loop if not.


RE: How to detect and tell user that no matches were found in a list - RedSkeleton007 - Jul-17-2018

(Jul-17-2018, 03:38 AM)micseydel Wrote: Write a function that returns true if you find it, and returns false after the loop if not.

Remember, we need the program to not just return false if no numbers in myList are less than userNumber, but also if that is the case AFTER we have reached the end of myList. I think I got that right on line 12 in the following code, but I'm not sure:

#!/usr/bin/env python3
#PracticePythonExercise03.py

myList = [0,2,5,4,6,7,9,8]

def seeIfThereIsAnElementSmallerThanUserNumber():
    userNumber = int(input("Pick a number: "))
    numbersWeAreLookingFor = []
    for i in myList:
        if i < userNumber:
            numbersWeAreLookingFor.append(userNumber)
            if i == myList[-1] and not numbersWeAreLookingFor:
                print("There were no numbers smaller than " + str(userNumber) + " in myList.")
            else:
                print("The numbers smaller than " + str(userNumber) + " were:")
                for x in numbersWeAreLookingFor:
                    print(x)

seeIfThereIsAnElementSmallerThanUserNumber()
Also, a test run of the code resulted in the following:
Error:
>>> ================================ RESTART ================================ >>> Pick a number: 3 The numbers smaller than 3 were: 3 The numbers smaller than 3 were: 3 3 >>>
What happened? The condition on line 10 is clearly supposed to look for numbers less than 3, NOT equal to 3, so what is up with that output?


RE: How to detect and tell user that no matches were found in a list - volcano63 - Jul-17-2018

Which tutorial are you using? Your names are extremely un-Pythonic - (and over-long)

Lines 12- must be moved out of the loop; first condition in line 12 then will be redundant


RE: How to detect and tell user that no matches were found in a list - woooee - Jul-17-2018

Using your first program and a function will yield the following. Also, using single letter variable names like i,l, or O is a bad habit to form as they can look like numbers.
myList = [0,2,5,4,6,7,9,8]
userNumber = int(input("Pick a number: "))
 
print("the numbers in myList that are smaller than the number you entered are:")
 
def find_smaller(user_num)
    for test in myList:
        if test < user_num:
            print("Found a number less than " + user_num + " which is: " + test)
            ## will exit on first number found so will
            ## not find any other smaller numbers
            return True

    ## none found
    return False 



RE: How to detect and tell user that no matches were found in a list - RedSkeleton007 - Jul-19-2018

woooee, I had to alter your code, because the user_num argument in your code was not defined.

Also, I managed to make my program work using an empty list called smallerNums:

#!/usr/bin/env python3
#PracticePythonExercise03.py

myList = [0,2,5,4,6,7,9,8]
#userNumber = int(input("Pick a number: "))

##def findSmaller():
##    userNumber = int(input("Pick a number: "))
##    for test in myList:
##        if test < userNumber:
##            print("Found a number less than " + str(userNumber) + " which is: " + str(test))
##            #return True
##        else:
##            return False
##        #if none are found, then return false.
##        #return False

def findSmaller2():
    userNumber = int(input("Pick a number: "))
    smallerNums = []
    for test in myList:
        if test < userNumber:
            smallerNums.append(test)
        
    if not smallerNums:#if smallerNums is empty
        print("No numbers smaller than " + str(userNumber) + " were found in myList.")
    else:
        print("The numbers smaller than " + str(userNumber) + " found in myList were:")
        for i in smallerNums:
            print(i)

findSmaller2()
But is there a way to accomplish the same result without storing every value from myList smaller than userNumber into another list, and that will not return true after only one value is found that fits the criteria?


RE: How to detect and tell user that no matches were found in a list - woooee - Jul-19-2018

You can return when the first smaller number is found. And you can return anything you want.
def findSmaller():
    myList = [0,2,5,4,6,7,9,8]
    userNumber = int(input("Pick a number: "))
    for test in myList:
        if test < userNumber:
            print("Found a number less than " + str(userNumber) + " which is: " + str(test))
            return True

## leaving these lines commented as it is in the wrong place
##        else:
##            return False
    #if none are found, then return false.
    print("No smaller numbers were found")
    return False