Exercise 14: Tips and Delivery
Let's use argv and raw_input to ask the user some special questions. Next exercise you will learn how to read and write files, this exercise is the basis of the next section. In this exercise we will use raw_input in a slightly different way, and let it play a simple > as a prompt. This is similar to some of the games, such as the Zork or Adventure two games.
1 fromSysImportargv2 3Script, user_name =argv4prompt ='>' 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_name9Likes =raw_input (Prompt)Ten One Print "Where Do you live%s?"%user_name Alives =raw_input (Prompt) - - Print "What kind of computer does you have?" theComputer =raw_input (Prompt) - - Print """ - alright, so-said%r about liking me. + You live in%r. Not sure where the IS. - And you have a%r computer. Nice. + """% (likes, lives, computer)
View Code
We set the user prompt to variable prompt so that we do not need to repeatedly enter the characters that prompt the user each time we use Raw_input. And if you want to change the prompt to a different string, you just have to change one location.
It's very handy.
The results you should see
When you run this script, remember that you need to assign your name to the script and let the argv parameter receive your name.
Bonus points Exercise
1. Check how Zork and Adventure are two games. See if you can download it to a version and then play it.
2. Change the prompt variable to a completely different content and run it again.
3. Add a parameter to your script and let your program use this parameter.
4. Make sure you understand the three quotation marks "" "can define a multiline string, and% is the formatting tool for the string.
Exercise Exercises
1.
Stupid way to learn Python (14)