"Learn Python the Hard Way"學(xué)習(xí)筆記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 \是什么

想象你有一個用雙引號引用起來的字符串亥揖,你想要在字符串的內(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()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末强霎,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蓉冈,更是在濱河造成了極大的恐慌城舞,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,627評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件寞酿,死亡現(xiàn)場離奇詭異家夺,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)伐弹,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評論 3 399
  • 文/潘曉璐 我一進(jìn)店門拉馋,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人惨好,你說我怎么就攤上這事煌茴。” “怎么了日川?”我有些...
    開封第一講書人閱讀 169,346評論 0 362
  • 文/不壞的土叔 我叫張陵蔓腐,是天一觀的道長。 經(jīng)常有香客問我龄句,道長回论,這世上最難降的妖魔是什么散罕? 我笑而不...
    開封第一講書人閱讀 60,097評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮傀蓉,結(jié)果婚禮上欧漱,老公的妹妹穿的比我還像新娘。我一直安慰自己葬燎,他們只是感情好误甚,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著萨蚕,像睡著了一般靶草。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上岳遥,一...
    開封第一講書人閱讀 52,696評論 1 312
  • 那天奕翔,我揣著相機(jī)與錄音,去河邊找鬼浩蓉。 笑死派继,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的捻艳。 我是一名探鬼主播驾窟,決...
    沈念sama閱讀 41,165評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼认轨!你這毒婦竟也來了绅络?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,108評論 0 277
  • 序言:老撾萬榮一對情侶失蹤嘁字,失蹤者是張志新(化名)和其女友劉穎恩急,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體纪蜒,經(jīng)...
    沈念sama閱讀 46,646評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡衷恭,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評論 3 342
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了纯续。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片随珠。...
    茶點(diǎn)故事閱讀 40,861評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖猬错,靈堂內(nèi)的尸體忽然破棺而出窗看,到底是詐尸還是另有隱情,我是刑警寧澤倦炒,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布显沈,位于F島的核電站,受9級特大地震影響析校,放射性物質(zhì)發(fā)生泄漏构罗。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評論 3 336
  • 文/蒙蒙 一智玻、第九天 我趴在偏房一處隱蔽的房頂上張望遂唧。 院中可真熱鬧,春花似錦吊奢、人聲如沸盖彭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,698評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽召边。三九已至,卻和暖如春裹驰,著一層夾襖步出監(jiān)牢的瞬間隧熙,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,804評論 1 274
  • 我被黑心中介騙來泰國打工幻林, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留贞盯,地道東北人。 一個月前我還...
    沈念sama閱讀 49,287評論 3 379
  • 正文 我出身青樓沪饺,卻偏偏與公主長得像躏敢,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子整葡,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評論 2 361

推薦閱讀更多精彩內(nèi)容