Let's create an example that combines argv and raw_input to lay the foundation for learning to read and write files in the next example. In this example, raw_input only gives a simple prompt, a bit like a game Zork or Adventure.
[Python]
1. from sys import argv
2.
3.
4. script, user_name = argv
5. prompt = '>'
6.
7.
8. print "Hi % s, I'm the % s script." % (user_name, script)
9. print "I 'd like to ask you a few questions ."
10. print "Do you like me % s? "% User_name
11. likes = raw_input (prompt)
12.
13.
14. print "Where do you live % s? "% User_name
15. lives = raw_input (prompt)
16.
17.
18. print "What kind of computer do you have? "
19. computer = raw_input (prompt)
20.
21.
22. print """
23. Alright, so you said % r about liking me.
24. You live in % r, Not sure where that is.
25. And you have a % r computer, Nice.
26. "" % (likes, lives, computer)
We have set a prompt variable and set the parameter in raw_input so that we don't need to write this parameter every time, and it is very convenient to modify it.
Running result
Root @ he-desktop :~ /Mystuff # python ex14.py eric
Hi eric, I'm the ex14.py script.
I 'd like to ask you a few questions.
Do you like me eric?
> Yes
Where do you live eric?
> China
What kind of computer do you have?
> Candy
Alright, so you said 'yes' about liking me.
You live in 'China', Not sure where that is.
And you have a 'candy 'computer, Nice.
Root @ he-desktop :~ /Mystuff #
Extra score exercise
1. Find Zork and Adventure games to play.
2. Change the variable prompt and execute it again.
3. Add another parameter to the program.
4. Confirm that "indicates multi-line output and % indicates the formatting media.
Author: lixiang0522