蟒蛇。来自特定单词的随机字母

时间:2015-11-24 21:19:05

标签: python

我正在使用python,我想制作一个刽子手游戏,我做了大部分。玩家1写入一个单词,然后玩家2将通过一个又一个字母来猜测它是什么单词,你总共有7次尝试。我现在唯一需要的东西可以从玩家2猜测的单词中随机选择一个字母。当玩家达到3次以下或类似的尝试时。我真的很新编码,我不知道多少例如我尝试过:if tries< 3:print(random.letter'wordmaking')其中wordmaking是输入的名称和玩家1输入的单词。让我们说你和朋友一起玩刽子手而你无法找出他用过的单词或字母。将会有一个代码,为您提供一个尚未猜到的字母:猜猜词:_ _ _ _你剩下4次尝试 让我们说这个单词是D E S K并且他输入了字母G,剩下的尝试将下降到3并且游戏将输入Guess字:D _ _ _你还有3次尝试。当他达到一定程度的尝试作为暗示时,它增加了一封信。就像画画一样,只有当你没有多少时间时,他们会给你提示。

3 个答案:

答案 0 :(得分:1)

>>> from random import choice
>>> some_word = 'wordmaking'
>>> choice(some_word)
'i'
>>> choice(some_word)
'k'
>>> choice(some_word)
'i'
>>> choice(some_word)
'g'

答案 1 :(得分:0)

那会对你想要做的事情有用吗?

import random

tries=0 #iterator initiation
if tries<=3:
    letter=random.choice("thewordyouwannaselectlettersfrom")
    print(letter)

else:
    tries+=1
    continue

答案 2 :(得分:0)

忍不住写一个刽子手:

import random

# take input for the word of player 1
target = input('Player 1 word: ')

# set guess to empty, but same length as 'target'
guess = '_'*len(target)
tries = 0

while (guess != target) and (tries < 3):
    # take input of player 2
    s = input('Now: "' + guess + '"; Player 2 can guess: ')

    # if guessed input is longer than 1 character, skip it
    if len(s) > 1:
        print('only 1 character allowed at a time')
        continue 

    # update guess with guessed letter if it hits
    guess = ''.join(s if target[i] == s else guess[i] for i in range(len(target)))

    # test if there were any correct guesses, otherwise tries += 1
    if not any(target[i] == s for i in range(len(target))):
        tries+=1

# print result after exiting loop
print('Player {} WINS: word was {}'.format('2' if tries < 2 else '1', target))