Reputation: 9
I'm a month into using Python, fairly new. I had a question regarding functions, and how to implement them. If this kind of question has been asked before, I apologize, and would appreciate it greatly if nudged in the right direction. Here is my code:
def getvalues(num1, num2):
for k in range(0, len(num1)):
if num1[k] > num2:
num1[k] = 0
num1 = int(input('Please enter a integer value, or quit to quit: '))
while num1 not in 'quit':
num1 = int(input('Please enter a integer value, or quit to quit: '))
if num1 in 'quit':
num2 = int(input('Please enter a threshold value: '))
getvalues
What I am trying to do is get the user to input an integer value, and then when the user types 'quit', it will ask for the threshold value, and then display only the integers above the threshold value. I believe the function to be correct, I am having trouble trying to implement the function. I put getvalues at the end to call the function. Should I ask for input after the function? Or is it possible to put it all in the function, then call it, and get user input that way? I want the data to be displayed in a list. I am stuck, and would greatly appreciate any help.
Upvotes: 0
Views: 271
Reputation: 104712
There are a bunch of issues with your current function. The biggest one seems to be confusion about variable names which are used for multiple different things in different places. You've also got all of your code nested in a way that I don't think is useful.
From your description, you want your code to go through two main steps. The first step is asking the user for several numbers (one at a time) until they enter quit
instead of a number. The second step is to ask for a threshold value, and to print out (or maybe return
?) all the values from the first step that are greater than or equal to it.
Here's how I'd do it:
def getvalues(): # note, no arguments, we gather all the data inside the function
values = [] # start with an empty list, with a good variable name
while True: # loop indefinitely
entry = input('Please enter a integer value, or quit to quit: ')
if entry == 'quit':
break # stop the loop if the user types quit instead of a number
values.append(int(entry)) # convert to a number only after checking for "quit"
threshold = int(input('Please enter a threshold value: ')) # use a good variable name
for value in values: # loop over the values gathered above
if value >= threshold: # compare each value to the threshold
print(value) # and print the ones that are larger
You could also reorganize things in a larger way. For instance, the filtering by threshold could be done in a separate function than asking the user for the input values. Or alternatively, if you asked for the threshold first, you could do the filtering at the same time you get the user input (immediately dropping values less than the threshold rather than filtering them out later).
Upvotes: 0
Reputation: 9858
You seem to want something like this. It may be easier to split the two things you want to do (get the values, then filter the ones that are higher than a threshold) into 2 functions. This may make it easier to understand how the programme flows too, rather than using nested for
loops which can get confusing.
First, we call get_values
to request the user to enter values and append them to a list inputted_values
. When the user enters quit we return that list. The second function main
calls get_values
then asks the user to enter the threshold, and then prints all the values that are greater than that threshold.
def get_values():
inputted_values = []
while True:
num = input('Please enter a integer value, or quit to quit: ')
if num == 'quit':
return inputted_values
inputted_values.append(int(num))
def main():
my_values = get_values()
threshold = int(input('Please enter a threshold value: '))
print([x for x in my_values if x > threshold])
main()
Note also that we don't convert the input in the first function to int
until after we have checked whether the user entered 'quit' (a string). Neither of the functions need parameters, because we are not passing any data to them, but we need to ensure that they return
or print
something, otherwise nothing will seem to happen.
Upvotes: 2