I took my basic Word jumble game and added some additional features to learn new stuff. My plan was not to have any pre-defined words coded in my python script but rather different group of words in a text file that are being imported into the game. In my case these were: sad_words.txt, amazement_words.txt.
I made interfaces for the user to add words, to browse the words by group name and to delete a specific group.
After importing a list of words, the user can play the game, first selecting a group of words. Then the user is given four jumbled words and at the end the score of the number of words he/she gets right.
The score for the most recent game is also recorded in a file that can be accessed by the user from the main menu.
For the storage of word groups and scores I used shelve.
import random
import shelve
# Welcome the player
print("""
Welcome to Word Jumble.
Unscramble the letters to make a word.
""")
def menu():
print("1--Play the game")
print("2--Browse a word set")
print("3--Add a new word set")
print("4--Delete a word set")
print("5--My sorted scores")
print("6--Exit")
pick = int(input("Pick one:"))
if pick == 1:
shelf = shelve.open("wordlists.dat")
for key in shelf.keys():
print(key)
word_set = input("Pick one:")
if word_set != key:
word_set = input("Type in the correct set: ")
global wordlist
wordlist = shelf[word_set]
game()
elif pick == 2:
browse()
elif pick == 3:
global name
name = input("Name: ")
filename = input("File name:")
add_list(filename)
elif pick == 4:
delete()
elif pick == 5:
score()
def browse():
print("Retrieving word list from shelf")
shelf = shelve.open("wordlists.dat")
for key in shelf.keys():
print(key)
name = input("Which one?")
if name != key:
name = input("Which one?")
print("Your words: {}".format(shelf[name]))
shelf.close()
return menu()
def game():
score = 0
for i in range(4):
word = random.choice(wordlist)
theWord = word
jumble = ""
while(len(word) > 0):
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[position + 1:]
print("The jumble word is: {}".format(jumble))
# Getting player's guess
guess = input("Enter your guess: ").lower()
# congratulate the player
if(guess == theWord):
print("Congratulations! You guessed it")
score += 1
else:
print("Sorry, wrong guess.")
print("You got {} out of 10".format(score))
shelf = shelve.open("score.dat")
shelf["score"]=[score]
shelf.sync()
shelf.close()
return menu()
def score():
shelf = shelve.open("score.dat")
for key in shelf.keys():
print(shelf[key])
shelf.close()
return menu()
def add_list(file):
with open(file) as afile:
the_list = [word.strip(",") for line in afile for word in line.split()]
print(the_list)
print("Shelving Lists ...")
shelf = shelve.open("wordlists.dat")
shelf[name] = the_list
shelf.sync()
shelf.close()
print("Success.")
print("Retrieving word list from shelf")
shelf = shelve.open("wordlists.dat")
print("Your words: {}".format(shelf[name]))
shelf.close()
return menu()
def delete():
shelf = shelve.open("wordlists.dat")
for key in shelf.keys():
print(key)
delete_key = input("Do you want to delete:")
if delete_key == key in shelf.keys():
del shelf[key]
shelf.sync()
shelf.close()
else:
print("Please type in the correct file")
return delete()
return menu()
menu()
Any other suggestions how to improve it (especially code structure and re-usability - should I use modules?) would be very helpful as I am a total beginner and began Python 6 weeks ago.