python - Run while loop one extra time after condition is met -


i making area calculator me understand basics of python, want type of validation on - if length less zero, ask again. i've managed 'validation' code inside function shape (e.g. inside 'square' function) when put validation code in separate function - 'negativelength,' doesn't work. code in separate function:

def negativelength(whichone):     while whichone < 1:         whichone = int(input('please enter valid length!')) 

when run calling 'negativelength(length)' ask me length again (as should) when enter positive length, condition met , actual loop not run.

i have tried (after following emulate do-while loop in python?)

def negativelength(whichone):     while true:         whichone = int(input('please enter valid length!'))         if whichone < 1:             break 

... doesn't work either.

i've put parameter 'whichone' because circle's 'length' called radius, i'd call negativelength(radius) instead of negativelength(length) square.

so there way make while loop finish after 'whichone = int(input...)'?

edit: i'm using python 3.3.3

the code you've written works, far goes. however, won't useful, because whichone never returned caller of function. note that

def f(x):     x = 2  x = 1 f(x) print(x) 

will print 1, not 2. want this:

def negative_length(x):     while x < 0:         x = int(input('that negative. please input non-negative length:'))     return x  x = input('enter length:') x = negative_length(x) 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -