1

I am making some homework drill and am trying to check whether a number is a positive integer using Try. somewhere along the way it returns the value of my input, but it returns the negative value, even after I eventually type in a valid value. lets say- first i typed in -10, it tells me to type again, then I type 2, but the return is still -10.

def takes_desicion(name):
    print "Ok %s, now lets make a desicion what to do with your text.\n" %name
    print "you can either- 1.leave it, 2.reverse it, or 3.make a new input."
    while True:
        try:    #takes an input with exception- in this case, only integer.
            desicion = int(raw_input("Hit your number to decide."))
            print desicion
        except ValueError:
            print "Sorry, thats not a number."
            continue
        else:   #if int is negetive, goes back to the function again, recursively
            if desicion < 1:
                print "That number is illegal."
                takes_desicion(name)
            break
    return desicion

if i first input -10, and the second input is 2, i would expect the output (return) to be 2. but its -10. That's the value that returns from the function.

Stan
  • 151
  • 7
  • See also [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). – glibdud May 22 '19 at 13:04

1 Answers1

0

You call the takes_desicion function again on third last line, but you don't do anything with the value from that function. You need to store the newly returned value: desicion = takes_desicion(name)

Although you should just use continue instead t jump to the first like of the while loop, there's no need to call the function again:

def takes_desicion(name):
    print "Ok %s, now lets make a desicion what to do with your text.\n" %name
    print "you can either- 1.leave it, 2.reverse it, or 3.make a new input."
    while True:
        try:    #takes an input with exception- in this case, only integer.
            desicion = int(raw_input("Hit your number to decide."))
            print desicion
        except ValueError:
            print "Sorry, thats not a number."
            continue
        else:   #if int is negetive, goes back to the function again, recursively
            if desicion < 1:
                print "That number is illegal."
                continue
            break
    return desicion
Markus Meskanen
  • 19,939
  • 18
  • 80
  • 119