我采用了我的基本Word混杂游戏,并添加了一些额外的功能来学习新东西。
import random
#from amazement import *
import shelve
# Welcome the player
print("""
Welcome to Word Jumble.
Unscramble the letters to make a word.
""")
def wordlist(file):
with open(file) as afile:
global the_list
the_list = [word.strip(",") for line in afile for word in line.split()]
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)
# print(shelf[key])
word_set = input("Pick one:")
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?")
print("Your words: {}".format(shelf[name]))
shelf.close()
return menu()
def game():
global score
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])
#print(shelf["score"])
shelf.close()
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()
#filename = "words/amazement_words.txt"
# wordlist(filename)
menu()
# game()
现在我在这里向有经验的编码员提出一些问题。
首先,当用户选择要播放的单词集时,他需要输入单词集。我认为在密钥中添加一个数字会更容易,例如:
1-amazement
2-sad
使用此功能将单词添加到游戏中:
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()
那么可以给密钥一个自动编号前缀吗?
搁置的第二个问题:当我玩游戏时,记录得分。在游戏结束时,它将保存到score.dat中。如果我退出程序并再次运行它,它可以工作。但是如果我想在程序运行时查看得分,在我完成游戏后,我会收到以下错误:
File "jumble_game.py", line 128, in <module>
menu()
File "jumble_game.py", line 33, in menu
game()
File "jumble_game.py", line 88, in game
return menu()
File "jumble_game.py", line 48, in menu
score()
TypeError: 'int' object is not callable
此外,我想保留最后10分,但现在只保留最新的分数。
任何其他建议如何改进它(特别是代码结构和可重用性 - 我应该使用模块吗?)将非常感激,因为我是一个初学者。
谢谢!