import sys
import requests
from bs4 import BeautifulSoup
from lxml import etree
import randomprint('\033[33m')
print('欢迎来到单词的世界!')
print('在这里,你将用给定的字母拼出单词,单词越长,得分越高')
print('现在好好享受单词的魅力吧')
print('\033[0m')
input('输入go开始游戏')
# 游戏模块
level = int(input('请选择难度(1~10):'))
# 初始字母数
start_num = int(26/level)
# 当前可用字母存放的地方
letter = []
# 已经拼写出来的单词存放的地方
words = []
# 26个字母列表
letter_list = []
# 元音字母列表
yuan_yin = ['a', 'o', 'i', 'e', 'u']
# 分数
score = 0
# 游戏是否继续
game = True
# 检查次数
check_no = 1
# 程序暂停
game_exit = 1
# 当前翻译
one = set()
# 字母修改次数
re_num = 11 - level
for i in range(97, 123): # 生成含有26个英文字母的列表letter_list.append(chr(i))
# 随机放入start_num个字母
for i in range(1, start_num):x = random.randrange(97, 123)letter.append(chr(x))x = random.randrange(97, 123)letter.append(chr(x))x = random.randrange(97, 123)letter.append(chr(x))x = random.randrange(97, 123)letter.append(chr(x))
for n in range(1, start_num):y = random.randrange(1, len(yuan_yin))letter.append(yuan_yin[y-1])
# 辅音字母列表
fu_yin = letter_list
for an in yuan_yin:if an in fu_yin:fu_yin.remove(str(an))def find_word(word): # 定义查找单词函数url = 'http://dict.youdao.com/w/eng/{}/#keyfrom=dict2.index'.format(word)website = requests.get(url).textglobal oneglobal transformhtml = BeautifulSoup(website, 'lxml')trans = html.find(class_='trans-container')if str(type(trans)) == 'NoneType':exit(0)else:transform = trans.find_all('li')if str(type(transform)) == 'NoneType':exit(0)for sa in transform:if type != 'NoneType':print('\033[36m' + sa.string + '\033[0m')else:return 0if len(transform) > 0:return wordelse:return 0def write(): # 输入单词并检查函数global game_exitnew_letter = letterfre = []# 检查是否在给出的字母表里for zi_mu in dan_ci:if zi_mu not in new_letter:print('字母列表中的字母{}数量不足'.format(zi_mu))for point in fre:letter.append(point)return 0else:new_letter.remove(zi_mu)fre.append(zi_mu)if dan_ci in words:print('单词已经拼写过了哦,请重新输入:')for yy in dan_ci:letter.append(yy)return 0return dan_cidef score_add(kg):# 积分程序score_now = 0if kg != 0 and dan_ci not in words:score_now = int(len(dan_ci) - 1)words.append(str(dan_ci))if score_now == 0:print('单个字母不计积分!')return 0elif score_now == 1:print('\033[31m两个字母的单词不计分\033[0m')return 0else:print('恭喜获得{}分'.format(score_now - 1))for poi in dan_ci:if str(poi) in letter:letter.remove(str(poi))for num in range(1, len(dan_ci)):nm_e = random.randrange(97, 123)print('\033[32m获得字母{}\033[0m'.format(chr(nm_e)))letter.append(chr(nm_e))return score_nowelif kg == 0:print('抱歉没找到这个单词:(')for sa in dan_ci:letter.append(sa)score_now = 0return score_nowdef ending(): # 游戏结束结算程序print('游戏结束!')print('您总共拼写出{}个单词'.format(len(words)))print('获得分数:{}分'.format(score))if input('是否查看所拼写的单词(y/n)') == 'y':for all_word in words:print('\033[31m' + all_word + '\033[0m', end=' ')sys.exit(0)def add_yuan(): # 检测列表中是否含有元音字母for yy in yuan_yin:if yy in letter:return 1return 0def print_letter(): # 打印当前可用字母print('\033[34m当前可用字母列表:\033[0m')na = -1bian_chang = int(pow(len(letter), 0.5))for hang in range(0, bian_chang + 1):for lie in range(0, bian_chang + 1):na += 1if na < len(letter):print(letter[na], end=' ')print()def word_replace(): # 单词替换程序global scoreglobal re_numif score >= 2:mode = str(input('替换:1,增加:2,减少:3'))if mode == '1':print('替换一个元音需2积分,替换一个辅音需4积分')re_word = input('请输入需要替换的字母:')if re_word in letter:if re_word in yuan_yin:if score >= 2:score = score - 2print('使用成功!')letter.remove(re_word)re_t = yuan_yin[random.randrange(0, 5)]else:print('对不起,您的积分不足!')print('当前积分:{}'.format(score))else:if score >= 4:score = score - 4print('使用成功!')letter.remove(re_word)re_t = fu_yin[random.randrange(0, 21)]letter.append(re_t)else:print('对不起,您的积分不足!')else:print('对不起,您输入的字母不在已有字母列表中')print('成功将' + str(re_word) + '替换成' + str(re_t))elif mode == '2':print('随机增加一个元音需要4积分,随机增加一个元音需要2积分')kind = input('随机增加元音:1,随机增加辅音:2')if kind == '1':add = random.randrange(0, 5)add_one = yuan_yin[add]print('成功增添字母{}'.format(add_one))letter.append(add_one)score -= 4elif kind == '2':add = random.randrange(0, 21)add_one = fu_yin[add]print('成功增添字母{}'.format(add_one))letter.append(add_one)score -= 2elif mode == '3':print('删除一个元音需要4积分,删除一个辅音需要2积分')kind_1 = input('请输入要删除的字母:')if kind_1 in letter:if kind_1 in yuan_yin:if score >= 2:print('删除成功!')score -= 2letter.remove(str(kind_1))else:print('对不起,您的积分不足')if kind_1 in fu_yin:if score >= 4:print('删除成功!')letter.remove(str(kind_1))score -= 4else:print('对不起,您的积分不足')else:print('请正确输入!(本次扣除1积分)')score -= 1elif mode == '':return 0elif re_num >= 0:print('由于积分不足,使用修改次数')print('当前积分外可用修改次数:{}次'.format(re_num))mode = str(input('替换:1,增加:2,减少:3'))if mode == '1':re_word = input('请输入需要替换的字母:')if re_word in letter:if re_word in yuan_yin:print('使用成功!')letter.remove(re_word)letter.append(yuan_yin[random.randrange(0, 5)])re_num -= 1else:print('使用成功!')letter.remove(re_word)letter.append(fu_yin[random.randrange(0, 21)])re_num -= 1else:print('对不起,您输入的字母不在已有字母列表中')elif mode == '2':kind = input('随机增加元音:1,随机增加辅音:2')if kind == '1':add = random.randrange(0, 5)u = yuan_yin[add]print('成功增加{}'.format(u))letter.append(u)re_num -= 1elif kind == '2':add = random.randrange(0, 21)letter.append(fu_yin[add])re_num -= 1elif mode == '3':kind_1 = input('输入要删除的字母')if kind_1 in letter:if kind_1 in yuan_yin:print('删除成功!')letter.remove(str(kind_1))re_num -= 1if kind_1 in fu_yin:print('删除成功!')letter.remove(str(kind_1))re_num -= 1else:print('请正确输入!(本次扣除1积分)')score -= 1re_num -= 1else:print('请正确输入!')return 0else:print('对不起,您的积分不足')print('当前积分{}'.format(score))print('当前积分外可用修改次数:{}次'.format(re_num))while game: # 主程序if add_yuan() == 0:letter.append(yuan_yin[random.randrange(0, 5)])print('列表中已没有元音字母,增加一个元音字母!')print_letter()print('\033[32m输入‘1’进行字母更改\033[0m')print('\033[32m输入“0”结束游戏\033[0m')print('\033[32m直接按回车随机添加一个字母,此操作需要有效的更改次数或者足够积分\033[0m')# 输入单词dan_ci = str(input('请输入单词:'))if dan_ci == '0':game = Falseending()elif dan_ci == '1':word_replace()elif dan_ci == '':if score > 0:score -= 1ins = random.randrange(97, 123)letter.append(str(chr(ins)))print('增添单词{}'.format(chr(ins)))print('使用1积分,当前积分{}分'.format(score))elif re_num > 0:re_num -= 1ins = random.randrange(97, 123)letter.append(str(chr(ins)))print('增添单词{}'.format(chr(ins)))print('使用积分外次数,当前可用积分外次数{}次'.format(re_num))else:print('\033[31m对不起,您的更改次数用完了\033[0m')else:# 执行检查程序# write()if write() != 0 and game_exit == 1:# 执行查找程序open_or_not = find_word(dan_ci)# 执行积分程序score = score + score_add(open_or_not)print('当前积分{}'.format(score))else:passif len(letter) == 0 or len(letter) == 1:print('恭喜你拼写完所有字母!')ending()
以上为全部代码,基本为现学现用,把代码放出来虚心求教,因为明天就期末考试了,相关解释之后想到就写(傲娇
本文链接:https://my.lmcjl.com/post/15548.html
展开阅读全文
4 评论