所以我基本上是在一个项目中,计算机从单词列表中提取一个单词,然后为用户弄乱它。 只有一个问题:我不想继续在列表中写很多单词,所以我想知道是否有一种方法可以导入很多随机单词,所以即使我也不知道它是什么,并且 那我也可以玩游戏吗? 这是整个程序的编码,我只输入了6个字:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31import random
WORDS = ("python","jumble","easy","difficult","answer", "xylophone")
word = random.choice(WORDS)
correct = word
jumble =""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
print(
"""
Welcome to WORD JUMBLE!!!
Unscramble the leters to make a word.
(press the enter key at prompt to quit)
"""
)
print("The jumble is:", jumble)
guess = input("Your guess:")
while guess != correct and guess !="":
print("Sorry, that's not it")
guess = input("Your guess:")
if guess == correct:
print("That's it, you guessed it!
")
print("Thanks for playing")
input("
Press the enter key to exit")
使用文字的文字文件? usrsharedictwords是常见的* nix平台,或者您可以使用其他单词列表...
可能会复制自由字词列表以进行编程使用?
阅读本地单词列表
如果您重复执行此操作,我将在本地下载并从本地文件中提取。 * nix用户可以使用/usr/share/dict/words。
例:
1
2word_file ="/usr/share/dict/words"
WORDS = open(word_file).read().splitlines()
从远程字典中提取
如果您想从远程词典中提取信息,可以采用以下两种方法。请求库使这变得非常容易(您必须pip install requests):
1
2
3
4
5
6import requests
word_site ="http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
response = requests.get(word_site)
WORDS = response.content.splitlines()
或者,您可以使用内置的urllib2。
1
2
3
4
5
6
7import urllib2
word_site ="http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()
非常感谢一个人,这确实有帮助,我把您的答案作为接受的答案,我投票了:)
请注意,链接http:www.freebsd.orgcgicvsweb.cgisrcsharedictweb2?rev=1.12;content-type=text%2Fplain现在已失效...
@Eric-:(猜猜我只能主持一个单词列表。
@Eric,这很好用; word_site =" svnweb.freebsd.org/csrg/share/dict/"。 它以ViewVc格式托管,他们提到下载链接是最好的选择。 permalink.gmane.org/gmane.comp.version-control.cvs.viewcvs.user/。 您能相信这本25k词典有21年历史了吗? 我喜欢网络。 // oop截断。 将编辑帖子。
Python 3解决方案
对于Python3,以下代码从网上获取单词列表并返回一个列表。答案基于上述Kyle Kelley接受的答案。
1
2
3
4
5
6import urllib.request
word_url ="http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain"
response = urllib.request.urlopen(word_url)
long_txt = response.read().decode()
words = long_txt.splitlines()
输出:
1
2
3
4>>> words
['a', 'AAA', 'AAAS', 'aardvark', 'Aarhus', 'Aaron', 'ABA', 'Ababa',
'aback', 'abacus', 'abalone', 'abandon', 'abase', 'abash', 'abate',
'abbas', 'abbe', 'abbey', 'abbot', 'Abbott', 'abbreviate', ... ]
并生成(因为这是我的目标)列表,这些列表包括:1)仅大写的单词,2)仅"类似于名称的单词"和3)听起来有点像现实但很有趣的随机名称:
1
2
3
4import random
upper_words = [word for word in words if word[0].isupper()]
name_words = [word for word in upper_words if not word.isupper()]
rand_name = ' '.join([name_words[random.randint(0, len(name_words))] for i in range(2)])
还有一些随机名称:
1
2
3
4
5
6
7
8
9
10
11
12
13>>> for n in range(10):
' '.join([name_words[random.randint(0,len(name_words))] for i in range(2)])
'Semiramis Sicilian'
'Julius Genevieve'
'Rwanda Cohn'
'Quito Sutherland'
'Eocene Wheller'
'Olav Jove'
'Weldon Pappas'
'Vienna Leyden'
'Io Dave'
'Schwartz Stromberg'
有一个软件包random_word可以非常方便地实现此请求:
1
2
3
4
5
6
7
8
9
10
11$ pip install random-word
from random_word import RandomWords
r = RandomWords()
# Return a single random word
r.get_random_word()
# Return list of Random words
r.get_random_words()
# Return Word of the day
r.word_of_the_day()
在线上有许多可用的字典文件-如果您使用的是Linux,则许多/ etc / dictionaries-common / words文件附带了很多(全部?)发行版,您可以轻松地解析(例如words = open('/etc/dictionaries-common/words').readlines())用来。
我在Windows 7上
好吧,谷歌说这里有一个词表:gutenberg.org/ebooks/3201
您可以编辑我的代码,以查看如果我想要本网站中的所有文字吗? freebsd.org/cgi/cvsweb.cgi/src/share/dict/
在线上网
1
2
3
4
5>>> words = requests.get("http://svnweb.freebsd.org/csrg/share/dict/words?view=co&content-type=text/plain").content.splitlines()
>>> x = 0
>>> for w in words:
... print(str(x) + str(w).replace("'b",""))
... x += 1
输出量
1
2
3
4
5
6
7
8
9
1025477b'zooplankton'
25478b'Zorn'
25479b'Zoroaster'
25480b'Zoroastrian'
25481b'zounds'
25482b"z's"
25483b'zucchini'
25484b'Zulu'
25485b'Zurich'
25486b'zygote'
将名称存储在本地PC中
1
2
3with open("dictionary.txt",'w') as file:
for w in words:
file.write(str(x) + str(w).replace("'b",""))