Posts: 84
Threads: 36
Joined: Jul 2017
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...
Posts: 2,342
Threads: 62
Joined: Sep 2016
Write a function that returns true if you find it, and returns false after the loop if not.
Posts: 84
Threads: 36
Joined: Jul 2017
(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?
Posts: 566
Threads: 10
Joined: Apr 2017
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.
Posts: 536
Threads: 0
Joined: Feb 2018
Jul-17-2018, 06:57 PM
(This post was last modified: Jul-17-2018, 06:57 PM by woooee.)
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
Posts: 84
Threads: 36
Joined: Jul 2017
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?
Posts: 536
Threads: 0
Joined: Feb 2018
Jul-19-2018, 06:27 PM
(This post was last modified: Jul-19-2018, 06:27 PM by woooee.)
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
|