Let's do one exercise that uses argv
and raw_input
together to
ask the user something specific. You will need this for the next exercise
where we learn to read and write files. In this exercise we'll use
raw_input
slightly differently by having it just print a simple >
prompt. This is similar to a game like Zork or Adventure.
.. literalinclude:: ex/ex14.py :linenos:
Notice though that we make a variable prompt
that is set to the prompt we
want, and we give that to raw_input
instead of typing it over and
over. Now if we want to make the prompt something else, we just
change it in this one spot and rerun the script.
Very handy.
When you run this, remember that you have to give the script your
name for the argv
arguments.
.. literalinclude:: ex/ex14.txt :language: console
- Find out what Zork and Adventure were. Try to find a copy and play it.
- Change the
prompt
variable to something else entirely. - Add another argument and use it in your script.
- Make sure you understand how I combined a
"""
style multi-line string with the%
format activator as the last print.