Stupid way to learn Python (14)

Source: Internet
Author: User

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)

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.