Exercise 14. Prompting and Passing

Let’s do one exercise that uses argv and input together to ask the user something specific. You will need this for the next exercise where you learn to read and write files. In this exercise we’ll use input slightly differently by having it print a simple > prompt. This is similar to games like Zork or Adventure.

ex14.py


 1    from sys import argv
 2
 3    script, user_name = argv
 4    prompt = '> '
 5
 6    print(f"Hi {user_name}, I'm the {script} script.")
 7    print("I'd like to ask you a few questions.")
 8    print(f"Do you like me {user_name}?")
 9    likes = input(prompt)
10
11    print(f"Where do you live {user_name}?")
12    lives = input(prompt)
13
14    print("What kind of computer do you have?")
15    computer = input(prompt)
16
17    print(f"""
18    Alright, so you said {likes} about liking me.
19    You live in {lives}.  Not sure where that is.
20    And you have a {computer} computer.  Nice.
21    """)

We make a variable, prompt, that is set to the prompt we want, and we give that to 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.

Exercise 14 Session


$ python3.6 ex14.py zed
Hi zed, I'm the ex14.py script.
I'd like to ask you a few questions.
Do you like me zed?
>  Yes
Where do you live zed?
>  San Francisco
What kind of computer do you have?
>  Tandy 1000

Alright, so you said Yes about liking me.
You live in San Francisco.  Not sure where that is.
And you have a Tandy 1000 computer.  Nice.

Study Drills

1. Find out what the games Zork and Adventure were. Try to find copies and play them.

2. Change the prompt variable to something else entirely.

3. Add another argument and use it in your script, the same way you did in the previous exercise with first, second = ARGV.

4. Make sure you understand how I combined a """ style multiline string with the {} format activator as the last print.

Common Student Questions

I get SyntaxError: invalid syntax when I run this script. Again, you have to run it right on the command line, not inside Python. If you type python3.6, and then try to type python3.6 ex14.py Zed, it will fail because you are running Python inside Python. Close your window and then just type python3.6 ex14.py Zed.

I don’t understand what you mean by changing the prompt? See the variable prompt = '> '. Change that to have a different value. You know this; it’s just a string and you’ve done 13 exercises making them, so take the time to figure it out.

I get the error ValueError: need more than 1 value to unpack. Remember when I said you need to look at the What You Should See section and replicate what I did? You need to do the same thing here, and focus on how I type the command in and why I have a command line argument.

How can I run this from IDLE? Don’t use IDLE.

Can I use double-quotes for the prompt variable? You totally can. Go ahead and try that.

You have a Tandy computer? I did when I was little.

I get NameError: name 'prompt' is not defined when I run it. You either spelled the name of the prompt variable wrong or forgot that line. Go back and compare each line of code to mine, starting at the bottom of the script and working up to the top. Any time you see this error, it means you spelled something wrong or forgot to create the variable.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.102.178