Python Forum
How to detect and tell user that no matches were found in a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to detect and tell user that no matches were found in a list
#1
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...
Reply
#2
Write a function that returns true if you find it, and returns false after the loop if not.
Reply
#3
(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?
Reply
#4
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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#5
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 
Reply
#6
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?
Reply
#7
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  
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  user input values into list of lists tauros73 3 1,064 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  detect equal sequences in list flash77 17 2,791 Oct-28-2022, 06:38 AM
Last Post: flash77
  functional LEDs in an array or list? // RPi user Doczu 5 1,590 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Split string using variable found in a list japo85 2 1,295 Jul-11-2022, 08:52 AM
Last Post: japo85
  Looking for a way to loop until user enters from a list? PythonW19 7 3,340 Mar-21-2021, 08:56 PM
Last Post: PythonW19
  User input/picking from a list AnunnakiKungFu 2 2,326 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
Question Reset list if user regrets Gilush 1 2,069 Dec-05-2020, 10:55 AM
Last Post: Gilush
  How can I found how many numbers are there in a Collatz Sequence that I found? cananb 2 2,539 Nov-23-2020, 05:15 PM
Last Post: cananb
  delete a Python object that matches an atribute portafreak 2 2,183 Feb-19-2020, 12:48 PM
Last Post: portafreak
  Help with calling list from user input farispython 5 2,791 Nov-03-2019, 03:13 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020