42

だから私は基本的に、コンピューターが単語のリストから単語を取り出し、ユーザーのためにそれをごちゃまぜにするプロジェクトに取り組んでいます。1つだけ問題があります:リストに大量の単語を書き続ける必要がないので、大量のランダムな単語をインポートして、それが何であるかさえわからない方法があるかどうか疑問に思っています.私もゲームを楽しめますか?これはプログラム全体のコーディングです。私が入力した単語は 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")
4

5 に答える 5

86

ローカル単語リストを読む

これを繰り返し行う場合は、ローカルにダウンロードして、ローカル ファイルからプルします。*nixユーザーが使用できます/usr/share/dict/words

例:

word_file = "/usr/share/dict/words"
WORDS = open(word_file).read().splitlines()

リモート辞書からプルする

リモート ディクショナリから取得する場合は、いくつかの方法があります。requests ライブラリを使用すると、これが非常に簡単になります (必要になりますpip install requests)。

import requests

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

response = requests.get(word_site)
WORDS = response.content.splitlines()

または、組み込みの urllib2 を使用できます。

import urllib2

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

response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()
于 2013-09-16T19:07:47.353 に答える
4

このリクエストを非常に便利に実装できるパッケージrandom_wordがあります。

 $ 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()
于 2019-07-01T07:33:06.057 に答える
3

オンラインで利用できる辞書ファイルは多数あります。Linux を使用している場合、多くの (すべての?) ディストリビューションに /etc/dictionaries-common/words ファイルが付属しており、これを簡単に解析 (words = open('/etc/dictionaries-common/words').readlines()など) して使用できます。

于 2013-09-16T18:20:08.180 に答える