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 \是什么
想象你有一個用雙引號引用起來的字符串亥揖,你想要在字符串的內(nèi)容里再添加一組雙引號進(jìn)去妙同,比如你想說 "I "understand" joe."砚蓬,Python 就會認(rèn)為 "understand" 前后的兩個引號是字符串的邊界矢门,從而把字符串弄錯。
解決方法1:
要解決這個問題灰蛙,你需要將雙引號和單引號轉(zhuǎn)義祟剔,讓 Python 將引號也包含到字符串里邊去。這里有一個例子:
"I am 6'2\" tall." # 將字符串中的雙引號轉(zhuǎn)義
'I am 6\'2" tall.' # 將字符串中的單引號轉(zhuǎn)義
解決方法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 參數(shù)仅父,解包叛薯,變量
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
像這樣執(zhí)行程序:
output13.png
Q:argv 和 raw_input()有什么區(qū)別?
它們的不同之處在于要求用戶輸入的位置不同。如果你想讓用戶在命令行輸入你的參數(shù)笙纤,你應(yīng)該使用argv.耗溜,如果你希望用戶在腳本執(zhí)行的過程中輸入?yún)?shù),那就要用到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)
執(zhí)行結(jié)果
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()
執(zhí)行結(jié)果
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()