"Learn Python the Hard Way"学习笔记3——Exercsie9-16

Exercise 9 输出输出输出

# Here's some new strange stuff,remember type it exactly.

days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"

print "Here are the days: ", days
print "Here are the months: ", months

print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""

Exercise 10 \是什么

想象你有一个用双引号引用起来的字符串,你想要在字符串的内容里再添加一组双引号进去,比如你想说 "I "understand" joe.",Python 就会认为 "understand" 前后的两个引号是字符串的边界,从而把字符串弄错。

解决方法1:
要解决这个问题,你需要将双引号和单引号转义,让 Python 将引号也包含到字符串里边去。这里有一个例子:

"I am 6'2\" tall."  # 将字符串中的双引号转义
'I am 6\'2" tall.'  # 将字符串中的单引号转义

解决方法2:
使用“三引号(triple-quotes)”,也就是 """,你可以在一组三引号之间放入任意多行的文字。

tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\cat."

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishes 
\t* Catnip\n\t* Grass
"""

print tabby_cat
print persian_cat
print backslash_cat
print fat_cat

Exercise 11 问问题

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)

Exercise 12 提示别人

ge = 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)

Exercise 13 参数,解包,变量

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 

像这样执行程序:


output13.png
Q:argv 和 raw_input()有什么区别?

它们的不同之处在于要求用户输入的位置不同。如果你想让用户在命令行输入你的参数,你应该使用argv.,如果你希望用户在脚本执行的过程中输入参数,那就要用到raw_input()。

Exercise 14 提示和传递

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)

执行结果


output14.png

Exercise 15 读取文件

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()

执行结果


output15.png

Exercise 16 读写文件

from sys import argv

script, filename = argv

print "We're going to erase %r." % filename
print "If you don't want to that, hit CTRL-C (^C)."
print "If you do want that, hit RETURN."

raw_input("?")

print "Opening the file..."
target = open(filename, 'w')

print "Truncating the file.  Goodbye!"
target.truncate()

print "Now I'm going to ask you for three lines."

line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")

print "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")

print "And finally, we close it."
target.close()

你可能感兴趣的:("Learn Python the Hard Way"学习笔记3——Exercsie9-16)