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!

Prune
  • 76,765
  • 14
  • 60
  • 81
  • You return the new value, but never assign to the old variable. – internet_user Mar 01 '18 at 21:29
  • please make sure that the while loop statement has the correct identation tab – enneppi Mar 01 '18 at 21:30
  • 4
    Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Mr. T Mar 01 '18 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 ? – kriss Mar 01 '18 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 – Jordan Williard Mar 01 '18 at 22:03
  • No, leave it here for further help to users having the same kind of trouble. They may stumble on your question and it will help them. SO is not temporary thread based, Q&A are here to stay. That being said, you could fix the question title (because it's the function not returning the expected value, it's not related to while loop at all). – kriss Mar 02 '18 at 10:17

1 Answers1

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.

kriss
  • 23,497
  • 17
  • 97
  • 116