python 2.78 如何编一个打乱单词的游戏?急,作业。
发布网友
发布时间:2022-04-27 08:37
我来回答
共1个回答
热心网友
时间:2022-06-29 08:51
你好
如果玩家猜错了单词呢? 希望可以多一些具体的要求
我在做了 默认的要求是这样
玩家看过五个单词 是包含那些give up的
并且 shuffle letters只能一次
import random
def shuffle(st):
string = ''
while st != '':
l = len(st)
x = int(random.random() * l)
char = st[x]
st = st[0:x] + st[x+1:l]
string = string + char
return string
word_list = ['give', 'put', 'hate', 'betrayal', 'embrassment', 'disappointment', 'fury', 'happiness', 'dream', 'opinion', 'behaviour']
print('Game begins :')
x = 0
score = 0
while True:
word = random.choice(word_list)
word_list.pop(word_list.index(word))
shuffled = shuffle(word)
print('The shuffled word is : ' + shuffled)
st = input('Type guess / shuffle letters / give up : ')
if st == 'guess':
w = input('Type the word : ')
w = w.rstrip()
if w == word:
print('Congrats!')
score += 1
else:
print('Wrong!')
elif st == 'shuffle letters':
shuffled = shuffle(shuffled)
print('The shuffled word is : ' + shuffled)
w = input('Please guess : ')
w = w.rstrip()
if w == word:
print('Congrats!')
score += 1
else:
print('Wrong!')
else:
print('You gave up.')
x += 1
if x == 5:
break
print('Game ends.')
print('Your score is : ' + str(score))追问如果猜错了就再给一个单词,猜错了也算那5次中的一次
追答是的 我就是这么做的