您的位置:首页 > 编程语言 > Python开发

Learn Python The Hard Way-Ecercise11~15

2014-01-25 13:28 621 查看
Exercise11: 提问

print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weight?",
weight = raw_input()

print "So, you're %r old, %r tall and %r heavy." %(age, height, weight)


输出结果:

$ python ex11.py

How old are you? 38

How tall are you? 6'2"

How much do you weight? 180lbs

So, you're '38' old, '6\'2"' tall and '180lbs' heavy.

分析:

这个练习通过raw_input()把输入的内容赋值给变量,从而实现交互。

Exercise12:提示别人

age = raw_input("How old are you?")
height = raw_input("How tall are you?")
weight = raw_input("How much do you weight?")

print "So, you're %r old, %r tall and %r heavy." %(age, height, weight)


输出结果:

$ python ex12.py

How old are you? 38

How tall are you? 6'2"

How much do you weight? 180lbs

So, you're '38' old, '6\'2"' tall and '180lbs' heavy.

分析:

在上个练习中的raw_input(),现在可以在括号里输入提示信息,告诉别人该输入些什么,让程序也更加简洁。

Exercise13:参数,解包,变量

from sys import argv #argv是参数变量

script, first, second, third = argv #将参数变量解包,将所有的参数依次赋予左边的变量名

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third


输出结果:
$ python ex13.py 1 2 3

The script is called: ex13.py

Your first variable is: 1

Your second variable is: 2

Your third variable is: 3

Exercise14:提示和传递
from sys import argv

script, user_name = argv
prompt = '>'

print "Hi %s, I'm the %s script." % (user_name, script)
print "I'd like to ask you a few questions."
print "Do you like me %s?" % user_name
likes = raw_input(prompt)

print "Where do you live %s?" % user_name
lives = raw_input(prompt)

print "What kind of computer do you have?"
computer = raw_input(prompt)

print """
Although, so you said %r about liking me.
You live in %r. No sure where that is.
And you have a %r computer. Nice
""" % (likes, lives, computer)


输出结果:
$ python ex14.py zed

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

Although, so you said 'Yes' about liking me.

You live in 'San Francisco'. No sure where that is.

And you have a 'Tandy 1000' computer. Nice

Exercise15:读取文件

from sys import argv

script, filename = argv #读取想要打开的文件名filename

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "Type the filename again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()


输出结果:

$ python ex15.py ex15_sample.txt

Here's your file 'ex15_sample.txt':

This is stuff I typed into a file.

It is really cool stuff.

Lots and lots of fun to have in here.

Type the filename again:

>  ex15_sample.txt

This is stuff I typed into a file.

It is really cool stuff.

Lots and lots of fun to have in here.

分析:

open( )--打开文件

.read( )--每次读取整个文件,它通常将读取到底文件内容放到一个字符串变量中,也就是说 .read() 生成文件内容是一个字符串类型
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: