Learn Python The Hard Way learning (14)-tips and passing

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.