Welcome, guest | Sign In | My Account | Store | Cart

Solve scrambled words. This game is made by a newbie to python so...

Python, 63 lines
 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
print "Rules"
print "\n"
print "This is how the game works..."
print "The game will give you a scrambled word. Then you 'guess' the original."
print "\n"
print "A couple of notes."
print "______________________________"
print "1: all words are never plural."
print "2: c() gives you the first letter."
print "3: s() surrenders. This also gives you the answer."

import random
def eq(str1, str2):
    str1=list(str1)
    str1.sort()
    str2=list(str2)
    str2.sort()
    if str1==str2:
        return True
    else:
        return False

word=["jazz", "quiz", "cozy", "whizz", "audio", "dark", "age", "sage", "silly", "fight", "flight", "kill", "queue", "queen", "crazy", "cab", "back", "stop", "ale", "soup", "pea", "mourn", "gym"]
while word==True:
    num=0
    printfa=True
    pick=random.choice(word)
    correct=pick
    pick=list(pick)
    while word==True: 
        random.shuffle(pick)
        if pick not in word:
            break
    a=len(correct)-2
    while True:
        print "\n"
        answer=raw_input("".join(pick)+": ")
        if eq(answer, correct) and answer in word:
            print "\n"
            print "You Win!"
            printfa=False
            break
        elif answer=="s()":
            break
        elif answer=="c()":
            print "\n"
            print "first letter: "+correct[0]
        else:
            print "\n"
            print "incorrect!"
            num+=1
            if num==a:
                break
            else:
                continue
    word.remove(correct)
    if printfa==True:
        print "________________"
        print "Ran out of tries"
        print "The correct answer was: "+correct
print "\n"
print "Sorry, no more words left!"
time.sleep(4)

Bug: empty list: Fixed

4 comments

Vimal 12 years ago  # | flag

line 27 throws an exception, checking list(word) is empty fixes the problem

random.choice([]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python2.5/random.py", line 248, in choice return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty IndexError: list index out of range

Vimal 12 years ago  # | flag

The comment appears really clumsy. My first comment in Active State, Sorry for that. This is more clear http://pastie.org/710896

Muhammet Kara 11 years, 10 months ago  # | flag

This is what I got when I try to run this code:

> Rules

This is how the game works... The game will give you a scrambled word. Then you 'guess' the original.

A couple of notes.


1: all words are never plural. 2: c() gives you the first letter. 3: s() surrenders. This also gives you the answer.

Sorry, no more words left!

Traceback (most recent call last): File "C:/Documents and Settings/Crispy/Desktop/py/word_puzzle.py", line 63, in <module> time.sleep(4) NameError: name 'time' is not defined

>

Seyhun Aky├╝rek 11 years, 7 months ago  # | flag

You need to import time module

Created by me me on Fri, 20 Nov 2009 (MIT)
Python recipes (4591)
me me's recipes (2)

Required Modules

Other Information and Tasks