Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
随机字生成器-Python_Python_Random_Python 3.3 - Fatal编程技术网

随机字生成器-Python

随机字生成器-Python,python,random,python-3.3,Python,Random,Python 3.3,所以我基本上是在做一个项目,计算机从一个单词列表中提取一个单词,然后为用户把它搞乱。只有一个问题:我不想一直在列表中写大量的单词,所以我想知道是否有办法导入大量随机单词,即使我不知道是什么,然后我也可以享受游戏?这是整个程序的编码,我只放了6个字: import random WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone") word = random.choice(WORDS) correct

所以我基本上是在做一个项目,计算机从一个单词列表中提取一个单词,然后为用户把它搞乱。只有一个问题:我不想一直在列表中写大量的单词,所以我想知道是否有办法导入大量随机单词,即使我不知道是什么,然后我也可以享受游戏?这是整个程序的编码,我只放了6个字:

import 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!\n")
print("Thanks for playing")

input("\n\nPress the enter key to exit")

在线提供了许多字典文件—如果您在linux上,许多(全部?)发行版都附带了/etc/dictionares common/words文件,您可以轻松解析(
words=open('/etc/dictionares common/words')。readlines()
,例如)以供使用

阅读本地单词表 如果您重复这样做,我会在本地下载它并从本地文件中提取*nix用户可以使用
/usr/share/dict/words

例如:

word_file = "/usr/share/dict/words"
WORDS = open(word_file).read().splitlines()
从远程词典中提取 如果您想从远程词典中提取数据,这里有几种方法。requests库让这变得非常简单(您必须
pip安装requests
):

或者,您可以使用内置的urllib2

import urllib2

word_site = "https://www.mit.edu/~ecprice/wordlist.10000"

response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()
在线获取单词 将前500个单词随机分组

from urllib.request import Request, urlopen
import random


url="https://svnweb.freebsd.org/csrg/share/dict/words?revision=61569&view=co"
req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})

web_byte = urlopen(req).read()

webpage = web_byte.decode('utf-8')
first500 = webpage[:500].split("\n")
random.shuffle(first500)
print(first500)
输出

[放弃]、[能够]、[土著]、[阿比让]、[阿比盖尔]、[阿比让]、[燃烧]、[废除]、[阿贝]、[上方]、[土著]、[土著]、[土著]、[阿伯丁]、[阿伯特]、[阿伯内西]、[阿博特]、[阿博特]、[阿博特]、[阿博特]、[阿博特]、[阿博特]、[阿博特]、[阿博特]、[阿博特]、[阿博特]、[阿博特]、[阿博特]、[阿博特]、[阿,“a”、“令人憎恶”、“阿贝尔”、“土豚”、“奥胡斯”、“阿贝”、“阿贝尔”、“阿贝尔”、“阿贝尔”、“教唆”、“阿巴斯”、“AAAS”、“退位”、“缩写”、“异常”、“卑鄙”、“算盘”、“遵守”、“可恶”、“住所”、“抛弃”、“阿巴斯”、“阿巴斯”、“腹部”、“教唆”、“阿巴斯”、“变态”、“腹部”、“教唆”、“富足”、“亚伦”、“厌恶”、“沐浴”、“教唆”,“关于”]

Python 3的解决方案 对于Python3,以下代码从web获取单词列表并返回一个列表

import 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()
输出:

>>> 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)一种真实但听起来有趣的随机名称:

import 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)])
还有一些随机的名字:

>>> 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'
有一个包可以非常方便地实现此请求:

 $ 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()

使用文字的文本文件?
/usr/share/dict/words
是常见的*nix平台,或者你可以使用其他的文字列表…可能的副本你可以编辑我的代码,看看如果我想要这个网站上的所有文字会是什么样子吗?谢谢很多人,这真的很有帮助,我把你的答案作为接受的答案,我投了赞成票:)注t点击链接
http://www.freebsd.org/cgi/cvsweb.cgi/src/share/dict/web2?rev=1.12;content type=text%2fplane
现在已经死了…@Eric-:(我想我只需要主持一个单词列表。@Eric,这很好用;word_site=“”。它采用ViewVc格式托管,他们提到下载链接是最好的方式;。你能相信这本25k字典已经有21年的历史了吗!!?我喜欢网络//oops截断。将编辑帖子。使用的链接似乎不推荐,请使用注意:这调用Wordnik.com上的API来获取随机单词。很好,但有点慢…需要导入什么
请求的必要信息
?导入请求我使用此代码获得了
404
,直到我设置
标题={'User-Agent':'Mozilla/5.0(Windows NT 6.1;Win64;x64)}
然后修改了代码
req=urllib.request.request(word\u url,headers=headers)
response=urllib.request.urlopen(req)
>>> 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'
 $ 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()