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
#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


Messages In This Thread
RE: How to detect and tell user that no matches were found in a list - by RedSkeleton007 - Jul-19-2018, 03:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  user input values into list of lists tauros73 3 1,252 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  detect equal sequences in list flash77 17 3,310 Oct-28-2022, 06:38 AM
Last Post: flash77
  functional LEDs in an array or list? // RPi user Doczu 5 1,873 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Split string using variable found in a list japo85 2 1,451 Jul-11-2022, 08:52 AM
Last Post: japo85
  Looking for a way to loop until user enters from a list? PythonW19 7 3,638 Mar-21-2021, 08:56 PM
Last Post: PythonW19
  User input/picking from a list AnunnakiKungFu 2 2,453 Feb-27-2021, 12:10 AM
Last Post: BashBedlam
Question Reset list if user regrets Gilush 1 2,163 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,674 Nov-23-2020, 05:15 PM
Last Post: cananb
  delete a Python object that matches an atribute portafreak 2 2,315 Feb-19-2020, 12:48 PM
Last Post: portafreak
  Help with calling list from user input farispython 5 2,993 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