Python实战(三)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/84196690

一 实战——输入

1 ex11.py

print "How old are you?",
age = raw_input()
print "How tall are you?",
height = raw_input()
print "How much do you weigh?",
weight = raw_input()
print "So, you're %r old, %r tall and %r heavy." % (
    age, height, weight)

2 测试结果

PS E:\Python\exercise> python ex11.py

How old are you? 36

How tall are you? 6'2"

How much do you weigh? 180lbs

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

二 实战——提示别人

1 ex12.py

age = raw_input("How old are you? ")
height = raw_input("How tall are you? ")
weight = raw_input("How much do you weigh? ")
print "So, you're %r old, %r tall and %r heavy." % (
    age, height, weight)

2 测试结果

PS E:\Python\exercise> python ex12.py

How old are you? 36

How tall are you? 173cm

How much do you weigh? 73kg

So, you're '36' old, '173cm' tall and '73kg' heavy.

三 实战——参数,解包,变量

1 ex13.py

from sys import 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

2 测试结果

PS E:\Python\exercise> python ex13.py I love python

The script is called: ex13.py

Your first variable is: I

Your second variable is: love

Your third variable is: python

四 实战——提示和传递

1 ex14.py

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 """
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives, computer)

2 测试结果

PS E:\Python\exercise> python ex14.py cakin

Hi cakin, I'm the ex14.py script.

I'd like to ask you a few questions.

Do you like me cakin?

> yes

Where do you live cakin?

> xian

What kind of computer do you have?

> x86

Alright, so you said 'yes' about liking me.

You live in 'xian'. Not sure where that is.

And you have a 'x86' computer. Nice.

五 实战——读文件

1 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.

2 ex15.py

from sys import argv
script, filename = argv
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()

3 测试结果

PS E:\Python\exercise> 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.

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/84196690