Learn Python The Hard Way-Ecercise11~15

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() 生成文件内容是一个字符串类型

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值