Question

I was banging my head against my desk on this one, because I'm an idiot, and I've finally gotten it to work. This is a simplified, dumb version of my actual code (which is why the function's purpose seems so inane) but I'm curious whether there is (or rather, am sure there must be) a more efficient way of doing the following:

def get_char():
    character = raw_input("Enter the character you would like to use.\n >")
    if character.isalpha:
        proceed = raw_input("The value entered is " + character + "\nWould you like to proceed?(Y/N)\n>")
        while True:
            if proceed in "N,n":
                raw_input("Press enter to try again with another value.")
                character = get_char()
                break
            else:
                break
    return character
    #print character

character = get_char()
print character

What I want to end up with is a way of checking that the user's input is what they intended. Until I set character = get_char() in the while loop, I was getting problematic output (i.e. incorrect final values for character); having fixed that, I've noticed that if I include the print statement at the end of the get_char() definition, it prints out the number of times a "no" choice has been made +1. While the end result is still fine, I'm curious whether the fact that it seems to be holding iterations in a queue, as indicated by the multiple prints upon inclusion of the print statement, means that there's a better way of doing this. Thanks in advance for any help!

UPDATE: Just in case anyone else needs help with this same issue, based on the suggestion by millerdev, I've adjusted the code to the following, which works just the same except without the self-call which was generating unnecessary character queuing:

def get_char():
    while True:
        character = raw_input("Enter the character you would like to use\n >")
        if character.isalpha:
            proceed = raw_input("The value entered is " + character + "\nWould you like to proceed? (Y/N)\n>")
            if proceed in ("N", "n"):
                raw_input("Press enter to try again with another value.")
            else:
                break
#print character    
return character

character = get_char()

Was it helpful?

Solution

Because of character = get_char(), your loop only runs once, because it will recurse deeper if it fails instead of iterating again. This probably isn't what you want, because it's slower and runs the risk of overflowing the stack. The cleanest way to do this would probably be (you could replace my messages with yours easily):

def get_char(is_valid = lambda c: True):
    while True:
        c = raw_input('enter a character > ')
        if is_valid(c):
            keep = raw_input('do you want to use {0}? (Y/N)> '.format(c)).lower()
            if 'n' in keep:
                continue # go back to While True line
            return c
        print('{0} is not a valid character'.format(c))

Session:

>>> get_char()
enter a character > a
do you want to use a? (Y/N)> n
enter a character > c
do you want to use c? (Y/N)> no
enter a character > x
do you want to use x? (Y/N)> y
'x'

At least, this is the cleanest in my opinion. Your implementation has a few other problems, like proceed in "N,n", which would also count comma as an n, and if the character isn't isalpha, you still return it.

OTHER TIPS

Is this what you're aiming for?

def get_char():
    while True:
        character = raw_input("Enter the character you would like to use.\n >")
        if character.isalpha: # this is always true; add parens if that's what you want, otherwise remove it
            proceed = raw_input("The value entered is " + character + "\nWould you like to proceed? (Y/n)\n>")
            if proceed.lower() == "n":
                raw_input("Press enter to try again with another value.")
                character = get_char()
            return character

character = get_char()
print character
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top