2013-09-16 83 views
18

Vì vậy, tôi về cơ bản đang làm việc trên một dự án nơi máy tính lấy một từ trong danh sách các từ và làm cho nó rung lên cho người dùng. chỉ có một vấn đề: tôi không muốn phải viết hàng tấn từ trong danh sách, vì vậy tôi tự hỏi liệu có cách nào nhập một tấn từ ngẫu nhiên cho nên thậm chí tôi không biết nó là gì, và sau đó tôi có thể thưởng thức các trò chơi quá? Đây là mã hóa của toàn bộ chương trình, nó chỉ có 6 từ mà tôi đặt trong:Trình tạo từ ngẫu nhiên- Python

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") 
+1

Sử dụng một file văn bản của từ? '/ usr/share/dict/words' là các nền tảng phổ biến * nix, hoặc có các danh sách từ khác mà bạn có thể sử dụng ... –

+0

có thể trùng lặp của [Danh sách từ miễn phí để sử dụng theo chương trình?] (http://stackoverflow.com/questions/ 772922/free-word-list-for-use-programatically) – Bakuriu

Trả lời

43

Đọc danh sách từ địa phương

Nếu bạn làm điều này nhiều lần, tôi sẽ tải xuống cục bộ và lấy từ tệp cục bộ. * Người dùng nix có thể sử dụng /usr/share/dict/words.

Ví dụ:

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

kéo từ một cuốn từ điển từ xa

Nếu bạn muốn kéo từ một cuốn từ điển từ xa, đây là một vài cách khác nhau. Thư viện yêu cầu làm cho điều này thực sự dễ dàng (bạn sẽ phải pip install requests):

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

Ngoài ra, bạn có thể sử dụng được xây dựng trong urllib2.

import 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() 
+1

Cảm ơn rất nhiều người, điều này thực sự hữu ích, tôi đặt câu trả lời của bạn câu trả lời được chấp nhận và tôi đã bỏ phiếu lên :) – Infamouslyuseless

+0

Lưu ý rằng liên kết 'http://www.freebsd.org/cgi/cvsweb.cgi/src/share/dict/web2?rev=1.12;content-type=text%2Fplain 'bây giờ đã chết ... – Eric

+0

@Eric - :(Đoán tôi sẽ chỉ cần lưu trữ một danh sách từ. –

1

Có một số các tập tin có sẵn từ điển trực tuyến - nếu bạn đang ở trên linux, rất nhiều (? Tất cả) distro đi kèm với tệp/etc/dictionary-common/words, bạn có thể dễ dàng phân tích cú pháp (words = open('/etc/dictionaries-common/words').readlines(), ví dụ) để sử dụng.

+0

Tôi đang ở trên windows 7 – Infamouslyuseless

+0

Vâng, Google cho biết có một danh sách từ ở đây: http://www.gutenberg.org/ebooks/3201 –

+0

bạn có thể chỉnh sửa mã của tôi không , để xem nó trông như thế nào nếu tôi muốn tất cả các từ từ trang web này? http://www.freebsd.org/cgi/cvsweb.cgi/src/share/dict/web2?rev=1.12;content-type=text%2Fplain – Infamouslyuseless

0

có được những lời trực tuyến

>>> 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 

Output

25477b'zooplankton' 
25478b'Zorn' 
25479b'Zoroaster' 
25480b'Zoroastrian' 
25481b'zounds' 
25482b"z's" 
25483b'zucchini' 
25484b'Zulu' 
25485b'Zurich' 
25486b'zygote' 

Store tên trong pc địa phương

with open("dictionary.txt",'w') as file: 
    for w in words: 
     file.write(str(x) + str(w).replace("'b",""))