04 Divisors <<
Previous 30 Pick Word
Exercise 30 (and Solution)
This exercise is Part 1 of 3 of the Hangman exercise series. The other exercises are: Part 2 and Part 3.
本練習是《 Hangman》練習系列3的第1部分。其他練習是:第2部分和第3部分。
In this exercise, the task is to write a function that picks a random word from a list of words from the SOWPODS dictionary. Download this file and save it in the same directory as your Python code. This file is Peter Norvig’s compilation of the dictionary of words used in professional Scrabble tournaments. Each line in the file contains a single word.
在本練習中,任務是編寫一個函數,該函數從SOWPODS詞典的單詞列表中選擇一個隨機單詞。下載此文件
並將其保存在與Python代碼相同的目錄中。此文件是Peter Norvig對專業Scrabble錦標賽中使用的單詞
詞典的彙編。文件中的每一行都包含一個單詞。
Hint: use the Python random library for picking a random word.
提示:使用Python隨機庫選擇一個隨機詞。
Aside: what is SOWPODS
什麼是SOWPODS
SOWPODS is a word list commonly used in word puzzles and games (like Scrabble for example). It is the combination of the Scrabble Player’s Dictionary and the Chamber’s Dictionary. (The history of SOWPODS is quite interesting, I highly recommend reading the Wikipedia article if you are curious.)
SOWPODS是單詞拼圖和遊戲中常用的單詞列表(例如,Scrabble)。它是Scrabble Player詞典和Chamber
詞典的組合。 (SOWPODS的歷史非常有趣,如果您感到好奇,我強烈建議閱讀Wikipedia文章。)
Previous exercises with similar concepts
以前的類似概念練習
In previous exercises, we have covered all the necessary skills needed to complete this exercise. Check out:
1.Exercise 22: read from a file
2.Exercise 9: generating a random number
3.Exercise 3: accessing an element of a list
在以前的練習中,我們介紹了完成此練習所需的所有必要技能。查看:
1.練習22:讀取文件
2.練習9:生成一個隨機數
3.練習3:訪問列表的元素
Brief concept review
簡要概念回顧
Reading a file
讀取文件
The Python way of reading a file from disk is to use the with construction together with a while loop:
從磁盤讀取文件的Python方法是將with結構與while循環一起使用:
with open('filename.txt', 'r') as f:
line = f.readline()
while line:
# do something to the line, for example
# saving it to disk
line = f.readline()
print("The entire file as been read!")
The file filename.txt should be in the same directory as your Python script.
文件filename.txt應與Python腳本位於同一目錄中
The idea behind the with construction is that it isolates any variables defined in the with line from the rest of your code. When you are dealing with files, this means properly opening and closing the files. The open line in the with statement actually does the opening of the file and saves the file object in the variable f. The 'r' passed into the open() function tells Python “open the file for reading”. If the 'w' argument is passed instead, Python will interpret this as “open the file and allow writing to it.”
with構造背後的想法是,它將with行中定義的所有變量與其餘代碼隔離開來。在處理文件時,這意味著正確
打開和關閉文件。 with語句中的打開行實際上是在打開文件並將文件對象保存在變量f中。傳遞給open()
函數的'r'告訴Python“打開文件進行讀取”。如果改為傳遞“ w”參數,Python會將其解釋為“打開文件並允
許對其進行寫入”。
After all the code inside the with block is finished, the file f is properly closed by Python. So by the time the program starts executing the print statement, the file is already closed.
在with塊中的所有代碼完成之後,文件f被Python正確關閉。因此,當程序開始執行print語句時,
該文件已經關閉。
One thing to remember when reading lines from a file in this way is that a line contains a \n, or a newline character at the end. So before processing the line, it is usually a good idea to use .strip() to remove the newlines and spaces from the start and end of the lines, like so:
以這種方式從文件中讀取行時要記住的一件事是,行的末尾包含\ n或換行符。因此,在處理行之前,通常
最好使用.strip()從行的開頭和結尾刪除換行符和空格,如下所示:
with open('filename.txt', 'r') as f:
line = f.readline().strip()
while line:
# do something with the 'line' variable
line = f.readline().strip()
There are many ways of reading lines from files. If for example you want to read all the lines of the file into a list, you can use the following variant:
有許多方法可以從文件中讀取行。例如,如果要將文件的所有行都讀入列表,則可以使用以下變體:
with open('filename.txt', 'r') as f:
lines = f.readlines()
At the end of this code, the variable lines will have all the elements from the file in a Python list.
Here are a few references about opening files, if you would like to read more:
在此代碼的結尾,變量行將在Python列表中包含文件中的所有元素。
如果您想了解更多信息,這裡有一些有關打開文件的參考:
.Learn Python the Hard Way page about reading files
.The Python 3.5 documentation about reading and writing files
在關於閱讀文件的困難方式頁面上學習Python
有關讀取和寫入文件的Python 3.5文檔
Generating a random number
產生一個隨機數
Python’s random library is used to do operations with random numbers. For a list of the full methods available in the random library, check the Python 3 documentation of the random library.
The two methods that might be useful for this exercise are random.choice() or random.randint(). The documentation is easy to read, so check out the docs for how these methods work!
Python的隨機庫用於對隨機數進行運算。有關隨機庫中可用的完整方法的列表,請查看隨機庫的Python 3
文檔。
對於本練習可能有用的兩個方法是random.choice()或random.randint()。該文檔易於閱讀,因此請
查看文檔以了解這些方法的工作方式
>>>>>>> 441673fc37d519665903229a4938dd860e65757e
04 Divisors <<
Previous