"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 \是什么

想象你有一個(gè)用雙引號(hào)引用起來的字符串,你想要在字符串的內(nèi)容里再添加一組雙引號(hào)進(jìn)去,比如你想說 "I "understand" joe.",Python 就會(huì)認(rèn)為 "understand" 前后的兩個(gè)引號(hào)是字符串的邊界,從而把字符串弄錯(cuò)。

解決方法1:
要解決這個(gè)問題,你需要將雙引號(hào)和單引號(hào)轉(zhuǎn)義,讓 Python 將引號(hào)也包含到字符串里邊去。這里有一個(gè)例子:

"I am 6'2\" tall."  # 將字符串中的雙引號(hào)轉(zhuǎn)義
'I am 6\'2" tall.'  # 將字符串中的單引號(hào)轉(zhuǎn)義

解決方法2:
使用“三引號(hào)(triple-quotes)”,也就是 """,你可以在一組三引號(hào)之間放入任意多行的文字。

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)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容