Learn Python The Hard Way-Ecercise11~15
2014-01-25 13:28
621 查看
Exercise11: 提问
输出结果:
$ 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:提示别人
输出结果:
$ 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:参数,解包,变量
输出结果:
$ 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:提示和传递
输出结果:
$ 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:读取文件
输出结果:
$ 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() 生成文件内容是一个字符串类型
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() 生成文件内容是一个字符串类型
相关文章推荐
- Learn Python The Hard Way-Ecercise36~40
- Learn Python The Hard Way学习(11) - 提问
- Learn Python The Hard Way-Ecercise16~20
- learning python in the hard way习题11~15的附加题练习
- Learn Python The Hard Way-Ecercise1~5
- Learn Python The Hard Way-Ecercise6~10
- Learn Python The Hard Way-Ecercise21~25
- 【Python】Learn Python the hard way, ex15 读取文件
- Learn Python The Hard Way学习(15) - 读取文件
- Learn Python The Hard Way-Ecercise26~30
- Learn Python The Hard Way-Ecercise31~35
- 【在路上】Learn Python The Hard Way 习题心得(ex11,12,13,14,15)
- Learn Python The Hard Way 15
- Learn Python The Hard Way学习(34) - 访问列表中的元素
- Learn Python The Hard Way学习(47) - 自动化测试
- 学习learn python the hard way习题46, ImportError问题
- 【python练习】《learn python the hard way》ex41 opp_test.py
- Learn Python The Hard Way-ex6
- learn python the hard way(笨办法学python) 练习38 附加练习
- 【Python】Learn Python the hard way, ex11 raw_input