笨辦法學python習題43
python版本:3 若有錯誤,敬請指出 模組名稱:測試.py
首先這個是錯的,PDF後面一段沒有....後找到補了上去,然而我的版本是3.5,還有些錯誤,解決不了,先掛著
#習題43
import random
from urllib.request import urlopen
import sys
WORO_URL = "http://learncodethehardway.org/words.txt"
WORDS = []
PHRASES = {
"class ###(###):":
"Make a class names ### that is_a ###.",
"class ###(object):\n\tdef__init__(self,***)":
"class ### has_a __init__that takes self and *** parameters",
"class ###(object):\n\tdef ***(self,@@@)":
"class ### has-a funcition names *** that takes self and @@@ parameters.",
"*** = ###()":
"Set *** to an instnce of class ###.",
"***.***(@@@)":
"From *** get the *** function,and call it with parameters self,@@@.",
"***.*** = '***':":
"From *** get the *** attribute and set it to '***'."
}
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
for word in urlopen(WORO_URL).readlines():
WORDS.append(word.strip())
def convert(snippet,phrase):
class_names = [w.capitalize() for w in
random.sample(WORDS,snippet.count("###"))]
other_names = random.sample(WORDS,snippet.count("***"))
result = []
param_names = []
for i in range(0,snippet.count("@@@")):
param_count = random.randint(1,3)
param_names.append(', '.join(random.sample(WORDS,param_count)))
for sentence in snippet,phrase:
result = sentence[:]
for word in class_names:
result = result.replace("###",word,1)
for word in other_names:
result = result.replace("***",word,1)
for word in param_names:
result = result.replace("@@@",word,1)
results.append(result)
return results
try:
while True:
snippets = (list(PHRASES.keys())[:])
random.shuffle(snippets)
for snippet in snippets:
phrase = PHRASES[snippet]
question,answer = convert(snippet,phrase)
if PHRASE_FIRST:
question,answer =answer,question
print(question)
input(">")
print("ANSWER: %s\n\n"% answer)
except EOFError:
print("\nBye")
相關文章
- 笨辦法學Python習題48Python
- 笨辦法學 Python · 續 練習 39:SQL 建立PythonSQL
- 笨辦法學前端前端
- 笨辦法學Python – 習題8-10: Printing & Printing, PrintingPython
- assert語句,python, 笨辦法學pythonPython
- 笨辦法學C 練習2:用Make來代替PythonPython
- 《笨辦法學Python》 第33課手記Python
- 笨辦法學C 練習28:Makefile 進階
- 笨辦法學C 練習13:Switch語句
- 笨方法學python加分習題18Python
- 笨方法學python加分習題19Python
- 笨辦法學C 練習25:變參函式函式
- 笨辦法學C 練習34:動態陣列陣列
- 笨辦法學C 練習42:棧和佇列佇列
- 笨辦法學C 練習29:庫和連結
- 笨辦法學C 練習18:函式指標函式指標
- 笨辦法學C 練習36:更安全的字串字串
- 笨辦法學C 練習8:大小和陣列陣列
- 笨辦法學 Golang 開山篇Golang
- 笨方法學Python3 習題3Python
- 笨辦法學C 練習38:雜湊演算法演算法
- 笨辦法學C 練習1:啟用編譯器編譯
- 每週一書:162頁《笨辦法學 Python》分享!Python
- 笨辦法學前端之圖片上傳前端
- 笨辦法學C 練習24:輸入輸出和檔案
- Learn Linux The Hard Way/笨辦法學LinuxLinux
- 笨辦法學C 練習17:堆和棧的記憶體分配記憶體
- 《“笨辦法”學Python(第3版)》點評贈書獲獎名單Python
- [人郵贈書]《“笨辦法”學Python(第3版)》點評贈書Python
- 笨辦法學C 練習7:更多變數和一些算術變數
- 前端除錯告別笨辦法前端除錯
- 《笨方法學python3》習題46.2 建立骨架專案目錄Python
- 笨辦法學C 練習45:一個簡單的TCP/IP客戶端TCP客戶端
- 求推薦,有沒有類似《笨辦法學 python》的書用來學 Java 的?PythonJava
- python, del[] 用法, 笨方法學pythonPython
- css居中辦法學習筆記CSS筆記
- libigl庫的學習筆記--問題與解決辦法筆記
- iOS學習筆記43 Swift(三)類iOS筆記Swift