0

I am making an area calculator to help me understand the basics of Python, but I want to do some type of validation on it - if a length is less than zero, then ask again. I've managed to do this with the 'validation' code inside the function for the shape (e.g. inside the 'square' function) but when I put the validation code in a separate function - 'negativeLength,' it doesn't work. This is my code in the separate function:

def negativeLength(whichOne):
    while whichOne < 1:
        whichOne = int(input('Please enter a valid length!'))

When I run this by calling 'negativeLength(Length)' it will ask me for the length again (as it should) but when I enter the positive length, the condition is met and so the actual loop does not run.

I have also tried (after following Emulate a do-while loop in Python?)

def negativeLength(whichOne):
    while True:
        whichOne = int(input('Please enter a valid length!'))
        if whichOne < 1:
            break

... but that doesn't work either.

I've put the parameter as 'whichOne' because the circle's 'length' is called Radius, so I'd call it as negativeLength(Radius) instead of negativeLength(Length) for a square.

So is there any way to make the while loop finish after the 'whichOne = int(input...)'?

Edit: I'm using Python 3.3.3

Community
  • 1
  • 1
ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38
  • It's helpful to see exactly what the code you're calling this from looks like. I suspect you're attempting to pass `Length` and `Radius` by reference, so that they're never actually getting set. – Scott Lawrence Apr 16 '14 at 15:54
  • @bytbox This is what I'm calling it from: `print('What is the length of one of the sides for your square, in cm?') Length = int(input()) negativeLength(Length) Area = Length * Length return Area` – ᔕᖺᘎᕊ Apr 16 '14 at 15:57

2 Answers2

1

The code you've written works, as far as it goes. However, it won't actually do anything useful, because whichOne is never returned to the caller of the function. Note that

def f(x):
    x = 2

x = 1
f(x)
print(x)

will print 1, not 2. You want to do something like this:

def negative_length(x):
    while x < 0:
        x = int(input('That was negative. Please input a non-negative length:'))
    return x

x = input('Enter a length:')
x = negative_length(x)
Scott Lawrence
  • 1,023
  • 6
  • 14
  • Thanks a lot, I didn't know that you could call a function as well as define it at the same time - you really helped, how do I mark this as answered? This is my first time in stackoverflow – ᔕᖺᘎᕊ Apr 16 '14 at 16:02
  • @shub I didn't even know how - had to look it up :) But it looks like you figured it out. – Scott Lawrence Apr 16 '14 at 16:05
0

I'm going to assume you're using Python 3. If not, you need to use raw_input() rather than input().

The code that I usually use for this would look like:

def negativeLength():
    user_input = raw_input('Please enter a length greater than 1: ')
    if int(user_input) > 1:
        return user_input
    input_chk = False
    while not input_chk:
        user_input = raw_input('Entry not valid.  Please enter a valid length: ')
        if int(user_input) > 1:
            input_chk = True
    return user_input

Which should do what you want.

Seth
  • 21
  • 1