Skip to content

Latest commit

 

History

History
42 lines (28 loc) · 1.26 KB

ex14.rst

File metadata and controls

42 lines (28 loc) · 1.26 KB

Exercise 14: Prompting And Passing

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.

What You Should See

When you run this, remember that you have to give the script your name for the argv arguments.

.. literalinclude:: ex/ex14.txt
    :language: console


Extra Credit

  1. Find out what Zork and Adventure were. Try to find a copy and play it.
  2. Change the prompt variable to something else entirely.
  3. Add another argument and use it in your script.
  4. Make sure you understand how I combined a """ style multi-line string with the % format activator as the last print.