0

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.

4
  • Please clarify, what exactly is the problem? Do you get an error? If so, please post it. If not then tell us, what does your code do and how does that differ from what you want it to do? Commented Jun 26, 2017 at 21:38
  • Also at the final line you need to add parentheses in order to call your function. Change getvalues to getvalues([argument 1 here], [argument 2 here]) Commented Jun 26, 2017 at 21:38
  • You're converting input to int and then comparing to 'stop', which won't work. And do you mean "stop" or "quit"? Commented Jun 26, 2017 at 21:39
  • I meant to put 'quit'; that's fixed, sorry. And since I am converting input to int, does that mean my "quit" command should be an integer of some sort? Commented Jun 26, 2017 at 21:45

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you! For clarity, by having two functions it makes it easier to understand. Because I want it inputted in a list, I assign inputted_values to [], which represents a list, while that is True the code asks to enter an integer value, if num is 'quit', it will return the inputted values. But wouldn't that end the code there? I am trying to get a better grasp of the concept. Do I call the get_values() function first, then call the main() function? Or will it run as it is written?
See my slight edit: you need to call main(), which in turn will call get_values(). The return in get_values just passes control back to main. The programme ends once the last command in main has been carried out.
why does calling main() call get_values() ?
The function main contains the line my_values = get_values(), which calls get_values
Oh I see. Thank you so much for your help. I think I understand the concept now.
0

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).

2 Comments

Your #comments are very helpful. I see a slight difference from your answer and the one above, he used two functions, while it seems you just have the one. Does that make a big difference? Or is it preference on how to format it?
You can divide things up as much or as little as you want. A common way to decide whether something should be in a separate function is whether it could be called from more than once place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.