单词接龙 python_Python单词接龙小程序

偶然间阅读资料发现Linux发行版内置了英语词典,随手用Python写个小程序进行词语接龙

规则:用户给出第一个词,系统根据结尾随机给出一个相同开头的词,如此反复

用户词不得重复,单词表中也不含缩写符号的词

代码如下

#!/usr/bin/python3

from random import sample

file = open(‘/usr/share/dict/words‘)

word = [x[:-1] for x in file.readlines() if "‘" not in x]

file.close()

# [word.remove(x) for x in word if x[-1]==‘s‘ and x[:-1] in word]

begin = False

com = ‘‘

used = []

num = 0

while True:

while True:

usr = input(‘Your word =‘)

if usr not in word:

print(‘Word‘,usr,‘not found!‘)

continue

elif usr in used:

print(‘You have used‘,usr,‘before!‘)

continue

else:

break

if begin and usr[0] != com[-1]:

print(‘Must begin with‘,com[-1])

continue

ans = [x for x in word if x[0]==usr[-1]]

com, = sample(ans,1)

num += 1

print(‘[Your turn %d‘%num, com)

used.append(usr)

begin = True

愉快的玩耍吧!实测s开头的特别多……

体验不佳可打开第7行注释过滤各类复数词,对应初始化时间也会随之增加。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值