2013-09-16 12 views
18

इसलिए मैं मूल रूप से एक प्रोजेक्ट पर काम कर रहा हूं जहां कंप्यूटर शब्दों की सूची से एक शब्द लेता है और उपयोगकर्ता के लिए इसे जुम्बल करता है। केवल एक समस्या है: मैं सूची में बहुत से शब्दों को लिखना नहीं चाहता हूं, इसलिए मैं सोच रहा हूं कि यादृच्छिक शब्दों का एक टन आयात करने का कोई तरीका है, इसलिए मुझे नहीं पता कि यह क्या है, और तो मैं भी खेल का आनंद ले सकता था? इस पूरे कार्यक्रम की कोडिंग है, यह केवल 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") 
+1

शब्दों का एक पाठ फ़ाइल का उपयोग में नाम ऑनलाइन शब्द मिलता है? '/ usr/share/dict/words' आम है * निक्स प्लेटफ़ॉर्म, या अन्य वर्डलिस्ट्स जिनका आप उपयोग कर सकते हैं ... –

+0

[संभवतः उपयोग के लिए निशुल्क शब्द सूची का संभावित डुप्लिकेट?] (http://stackoverflow.com/questions/ 772 9 22/फ्री-वर्ड-लिस्ट-फॉर-यूज-प्रोग्रामेटिकली) – Bakuriu

उत्तर

43

एक स्थानीय शब्द सूची

पढ़ा जाए तो यह बार-बार कर रहे हैं, मैं इसे स्थानीय स्तर पर डाउनलोड करने और स्थानीय फ़ाइल से खींच जाएगा। * निक्स उपयोगकर्ता /usr/share/dict/words का उपयोग कर सकते हैं।

उदाहरण:

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

एक दूरस्थ शब्दकोश

से खींच आप एक दूरस्थ शब्दकोश से खींचने के लिए चाहते हैं, तो यहां कई तरीकों से कर रहे हैं। अनुरोध पुस्तकालय यह वास्तव में आसान बना देता है (आप 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() 

वैकल्पिक रूप से, आप 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

धन्यवाद बहुत आदमी, यह वास्तव में मदद करता है, मैंने आपका जवाब उतना ही रखा स्वीकृत उत्तर और मैंने मतदान किया :) – Infamouslyuseless

+0

ध्यान दें कि लिंक 'http://www.freebsd.org/cgi/cvsweb.cgi/src/share/dict/web2?rev=1.12 ;content-type=text%2Fplain 'अब मर चुका है ... – Eric

+0

@Eric - :(मुझे लगता है कि मुझे सिर्फ एक शब्द सूची होस्ट करनी होगी। –

1

शब्दकोश फ़ाइलों को ऑनलाइन उपलब्ध की एक संख्या हैं - आप लिनक्स पर कर रहे हैं, का एक बहुत (? सब) distros एक/etc/शब्दकोश-आम/शब्द फ़ाइल के साथ आते हैं, जिसे आप आसानी से पार्स (words = open('/etc/dictionaries-common/words').readlines(), उदाहरण के लिए) उपयोग कर सकते हैं।

+0

मैं विंडोज 7 – Infamouslyuseless

+0

पर हूं, ठीक है, Google का कहना है कि यहां एक शब्दसूची है: http://www.gutenberg.org/ebooks/3201 –

+0

क्या आप अपना कोड संपादित कर सकते हैं , यह देखने के लिए कि अगर मैं इस वेबसाइट से सभी शब्द चाहता हूं तो यह कैसा लगेगा? http://www.freebsd.org/cgi/cvsweb.cgi/src/share/dict/web2?rev=1.12 ;content-type=text%2Fplain – Infamouslyuseless

0

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

आउटपुट

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

स्टोर स्थानीय पीसी

with open("dictionary.txt",'w') as file: 
    for w in words: 
     file.write(str(x) + str(w).replace("'b","")) 
संबंधित मुद्दे