Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Scramble word game
#1
def display_banner():
    print("""
 __                               _      _            _ 
/ _\  ___  _ __  __ _  _ __ ___  | |__  | |  ___   __| |
\ \  / __|| '__|/ _` || '_ ` _ \ | '_ \ | | / _ \ / _` |
_\ \| (__ | |  | (_| || | | | | || |_) || ||  __/| (_| |
\__/ \___||_|   \__,_||_| |_| |_||_.__/ |_| \___| \__,_|
                                                        
""")

def load_words(filename):
    #load file containing scrambled word and answer.
    #scrambled word and answer are separated by :

    scrambled_list = []
    answer_list = []
    with open('halloween.txt', 'r') as f:
        for line in f:
            (s,a) = line.strip().split(":")
            scrambled_list+=[s]
            answer_list+=[a]
    return (scrambled_list, answer_list)
            
            
def main():
    
    display_banner()
    import random
    file = open('halloween.txt', 'w')
    file.write('bta:bat\n')
    file.write('gstoh:ghost\n')
    file.write('enstrom:monster\n')
    file.write('ihtcw:witch\n')
    file.write('meizob:zombie\n')
    file.write('enetskol:skeleton\n')
    file.write('rpamevi:vampire\n')
    file.write('wbe:web\n')
    file.write('isdepr:spider\n')
    file.write('umymm:mummy\n')
    file.write('rboom:broom\n')
    file.write('nhlwaeeol:halloween\n')
    file.write('pkiumnp:pumpkin\n')
    file.write('kaoa jlern tcn:jack o lantern\n')
    file.write('tha:hat\n')
    file.write('claabck t:black cat\n')
    file.write('omno:moon\n')
    file.write('aurdclno:cauldron\n')
    file.close()
    done = False
    while not done:
        (scrambled_list, answer_list) = load_words('halloween.txt')
        file = open('halloween.txt', 'r')
        scrambled=random.choice(scrambled_list) #pick one word randomly from list created from file
        user_guess = answer_list
        print('scrambled word is:', scrambled)
        guess=input('What is the word?')
        if guess != user_guess:
            print('Wrong answer. Try again!')
        elif guess == user_guess:
            print('You got it!')
            another_game=input('Another game? (Y/N):')
            if another_game == 'Y' or 'y':
                continue
            if another_game == 'N' or 'n':
                print('Bye')
                done = True
    #--------------------------
    # Randomly pick a scrambled word from the list.
    # Asks the user to guess it.
    # Ask again if the guess is wrong.  Rpeat until the guess is right.
    # If guess is right, ask if user wants another game.
    #--------------------------
        
main()
I am having a issue when it checks the users guess of the scrambled word that the program gives you and does not compare it to the right word.
So it just keeps looping like this.
scrambled word is: rboom
What is the word?broom
Wrong answer. Try again!
scrambled word is: pkiumnp
What is the word?pumpkin
Wrong answer. Try again!
scrambled word is: claabck t
What is the word?
Reply
#2
guess is always the whole answer list, not a single item

https://python-forum.io/Thread-Multiple-...or-keyword
Reply
#3
Thanks for pointing that out..but my program is not making it to that part because of the question I asked above.
and I can fix that part with by doing this. That was just a simple mistake on my part.
 if another_game == 'Y' or another_game == 'y':
                continue
            if another_game == 'N' or another_game == 'n':
                print('Bye')
                done = True

(Feb-04-2018, 09:22 PM)Zatoichi Wrote: Thanks for pointing that out..but my program is not making it to that part because of the question I asked above.
and I can fix that part with by doing this. That was just a simple mistake on my part.
 if another_game == 'Y' or another_game == 'y':
      continue
 if another_game == 'N' or another_game == 'n':
      print('Bye')
      done = True
Reply
#4
I edited my answer, maybe you didn't see it. guess is always the whole answer_list, not a single element of it
Reply
#5
okay, so how do I go about making sure it chooses the the right word from the answer list? and I having another issue when the wrong word is entered I want it to ask again until the user gets it correct.
Reply
#6
a=(randrange(0,len(scrambled_list)-1))

print(scrambled_list[a])

guess=input("Enter your guess\n")

if guess == answer_list[a]:

try that in place of your scrambled_list and answer_list
Reply
#7
Thanks! solved!
Reply
#8
Please show your code, using Python tags (BBCode).

Two things. First, it's great that you searched for an existing thread to see if your question has already been answered. But, second, generally don't then want to add to a 3 year old thread - would be better to start a new thread rather than taking this one over (Zombie thread).
Reply
#9
Posting the code means posting your program, using the Python tags in the edit bar. Then we can see your code and make suggestions.
The person who started this thread and who you want an answer from has not been on this site since March of 2018, which is why I suggested that you start a new thread with your question.
Reply
#10
Difficult to tell you where your error is when you won't show your code. Without more info I would include letter counts. So, you know you have the letter "O" and it is associated with a letter count of 2. A dictionary may be useful for this purpose, with the key being the letter and the value being the count.
Otherwise we cannot help without you posting your code. That is why you get no responses.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need some help creating a word game wthiierry 4 2,332 Nov-01-2022, 12:29 PM
Last Post: perfringo
  Word based Game of 21 DatNerdKid 2 65,704 Aug-24-2018, 03:25 PM
Last Post: DuaneJack
  Scramble and Interleave in Python dsaks 5 5,027 Jun-13-2018, 10:10 PM
Last Post: dsaks
  scrabble word game atux_null 4 8,674 Nov-10-2017, 10:00 AM
Last Post: atux_null

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020