1

Just recently started learning python and ran into a bit of an odd problem. I've created this function to ensure that the number entered by a user is larger than zero:

def numcheck(value,variable):

    while value <= 0:
        print('The' +str(variable) + 'must be greater than zero! ')
        value = float(input('Please enter a positive number for' +str(variable)+ \
                            'and press enter. '))
    return value

The function is used within a main function as follows:

def main():
    length = float(input('What is the length of the block of cheese? '))
    numcheck(length,' length of the block ' )

My problem is that the function successfully recognizes non-positive values, but when a new value is entered, the original number is still passed to the rest of the function. For example, if -10 is entered into length, I will be prompted to enter another value. If 15 is entered, the while loop will close but -10 will still pass from the function completely ruining the arithmetic that follows. I have spent several hours trying to remedy this with no success. I would appreciate any advice y'all have to offer. Thanks so much!

6
  • You return the new value, but never assign to the old variable. Commented Mar 1, 2018 at 21:29
  • please make sure that the while loop statement has the correct identation tab Commented Mar 1, 2018 at 21:30
  • 4
    Possible duplicate of Asking the user for input until they give a valid response Commented Mar 1, 2018 at 21:30
  • Yes, it's not a very good question. But does question downvoters really want a newbie asking his first question in SO to immediately lose all his rep ? Maybe we should leave him some time to get used to SO before excluding him ? Commented Mar 1, 2018 at 21:41
  • 1
    My apologies for the quality of the question. I've been frustrated on this for several hours and just needed some assistance. The problem was fixed thanks to kriss, should I delete the thread? Obviously very new to SO Commented Mar 1, 2018 at 22:03

1 Answer 1

3

Python is pass by value. That means that the value of the variables passed to a function will be copied. If you modify a parameter variable of a function, it won't be changed outside it.

You just have to return the new length at the end of the function and call like:

length = numcheck(length,' length of the block ')

That way, you'll store the result in the length variable.

It's also not a very good practice to do your input both inside and outside the function. Duplicating the call to input is not really a problem, but both calls should be inside the function, the current way is ugly. But that's not the problem here.

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

Comments

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.