Prompting and passing in Python
Let’s do one exercise that uses argv and raw_input together to ask the user something specifi c. You will need this for the next exercise, where we learn to read and write fi les. 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.
1 from sys import argv 2 3 script, user_name = argv 4 prompt = '> ' 5 6 print "Hi %s, I'm the %s script." % (user_name, script) 7 print "I'd like to ask you a few questions." 8 print "Do you like me %s?" % user_name 9 likes = raw_input(prompt) 10 11 print "Where do you live %s?" % user_name 12 lives = raw_input(prompt) 13 14 print "What kind of computer do you have?" 15 computer = raw_input(prompt) 16 17 print """ 18 Alright, so you said %r about liking me. 19 You live in %r. Not sure where that is. 20 And you have a %r computer. Nice. 21 """ % (likes, lives, computer)
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.
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.
- 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 multiline string with the % format activator as the last print.
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 WYSS 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.