2

So I'm trying to get this function to return what I write only if it is equal to a character in an array. If not I want it to keep looping.

def pick():
    picks = input('Type one letter. ')
    choice = {'q', 'w', 'e', 'r', 't', 'y'}
    for x in choice:
        while x in choice != picks:
            picks = input('Pick again. ')
        else:
            return x
pick()

I am just getting really confused with this.

Example:

Type one letter. z

Pick again. q

then it will return q to the function to be used in another function.

Also It has to only continue to the next function if this one is right (returns the proper character). The other function while compare this functions answer to its own. Will it stop other functions from "starting" while this one keeps looping if it is not right?

2
  • 5
    Can you explain what are you expecting while x in choice != picks: to do? Commented Jan 18, 2016 at 0:31
  • If you were calling this a lot of times, it might be worth defining choice outside the pick function. Commented Jan 18, 2016 at 0:44

2 Answers 2

3

It is unnecessary to loop through choices every time. Also I'm not quite sure what you're trying to do with your while condition, but something like this should do the trick. The motif x in y returns a boolean which indicates if x is a member of y.

def pick():

    picks = input('Type one letter. ')
    choice = {'q', 'w', 'e', 'r', 't', 'y'}

    while picks not in choice:
        picks = input('Pick again. ')

    return picks

pick()

Also I would probably do a little bit to cleanup the user's input.

while picks.lower().rstrip() not in choice:
Sign up to request clarification or add additional context in comments.

3 Comments

While your answer is greatly improved over OP's original code, it doesn't run: NameError: name 'q' is not defined. This is primarily because of the way input() is being used instead of raw_input().
@Will it's a python 3.x question in which the behavior of input() is different. Oh the joys of 2.x vs 3.x
Oh, fun! Fixed my answer and included Python 2 and 3 examples then :) Sorry, upvoted!
1

Try (Python 3):

def pick():
    choices = {'q', 'w', 'e', 'r', 't', 'y'}

    picked = input('Type one letter: ')

    while picked not in choices:
        picked = input('Pick again: ')

    return picked

pick()

For Python 2:

def pick():
    choices = {'q', 'w', 'e', 'r', 't', 'y'}

    picked = raw_input('Type one letter: ')

    while picked not in choices:
        picked = raw_input('Pick again: ')

    return picked

pick()

You don't need to iterate through choices every time as in checks against every member of the set.

When you use input() in Python 2, Python will try to interpret the input as a variable, but raw_input() will return a string.

And, yes, this function will block until valid input is received, stopping subsequent functions from executing.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.