[Tutor] Python user input and return

Alan Gauld alan.gauld at yahoo.co.uk
Sun Sep 5 08:35:13 EDT 2021


On 04/09/2021 18:31, chandar pass wrote:
> I am trying to have a user pick an item from a list I have created, and if
> it is in the list they get a response, if not they should try again. I am
> having a hard time with this one for some reason.  Here is what I have so
> far.  all of my "user_choice" gets returned. Any kind of help would be
> greatly appreciated. Thanks
> 
> user_choice = ("kimura", "wrist lock", "cross choke")

Here you create a tuple (not the same as a list, terminology
is very important in programming!) with your options and give
it the name user_choice,

[ user_choices would be a better name since there are multiple
  items. Having good names makes code much easier to read.]

> input("What is your second favorite Jits move? :")

Now you ask the user for a choice. but you do not store
that choice anywhere. Python is not clever enough to know
where you want to store it, you have to give it a
variable name, something like:

option = input(...)

> print("{} is a sweet submission!".format(user_choice))

And here you print a result. But what you print is user_choice.
That is the name of the tuple, not what the user entered
(which you haven't stored anywhere)

So far you haven't tackled the problem of determining
if the user entered something in the tuple. That's OK,
it's good practice to build your programs in small steps.
Once you get the users choice printed you can add a
check to see if it is valid.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list