Im only comfortable with for loops at the moment so I've tried what I can there with minimal success. If I asked the user if they want A, B, or C and the user inputs D, instead of just saying it is invalid and moving on, i'd like the program to ask again until it gets a valid answer using raw_input.
Thanks!
解决方案
Here is an anonymous function that does what you want.
def get_user_choice(prompt, choices):
while True:
choice = raw_input(prompt)
if choice in choices:
return choice
else:
print 'choice must be in: {}'.format(choices)
Using it:
>>> get_user_choice('choose an option in A, B, C: ', ['A', 'B', 'C'])
choose an option in A, B, C: A
'A'
>>> get_user_choice('choose an option in A, B, C: ', ['A', 'B', 'C'])
choose an option in A, B, C: D
choice must be in: ['A', 'B', 'C']
choose an option in A, B, C: B
'B'
Note: I have not given too much information to help you, because I believe you should figure most of this out on your own, but meanwhile you should have some kind of working solution.