刽子手游戏帮助,改变“?????”一次一个字母,以显示部分猜测的单词

时间:2017-07-14 19:04:02

标签: python

我正在做一个刽子手游戏作为python中的初学者项目。我遇到了一个问题,这让我疯了。我想把这个词显示为????当用户猜到正确的字母时,它将取代“?”在正确的位置使用正确的字母。

示例:

秘密词是????

“用户输入f”

你是对的

这是你到目前为止f ???

到目前为止,这是整个游戏的代码

list(hiddenword)
hiddenword=[i if x==str("?") else x for x in hiddenword]
hiddenguessed= ''.join(hiddenword)
print('Here is what you have so far '+ str(hiddenguessed))

我试图从字符串中制作一个列表并翻转各个字符,但我没有运气。

{{1}}

这是我能做的最好的,但它所做的只是取代所有“?”带着猜对的信。虽然它只是这样做,如果它是正确猜测的信任何想法?

3 个答案:

答案 0 :(得分:1)

这个答案并不包含所有修复的错误 - 只需要很少的数量就可以运行它。

import random                                                                                        

print('Welcome to hangman')                                                                          
print('Type one of the following catagories')                                                        

Animal=['Cat','Dog','Bird','Cow','Fish','Lizard']                                                    
Clothing=['Shirt','Jeans','Sweatshirt','Shoes','Hat','Scarf']                                        
Weather=['Rain','Snow','Sunny','Sleet','Windy','Stormy']                                             
Colors=['Red','Blue','Green','Purple','Yellow','Grey']                                               

Alphabet= ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p',
           'q','r','s','t','u','v','w','x','y','z']                                                                                      

print('Type 1 for Animal, 2 for Clothing, 3 for Weather, 4 for Colors')                             

catagory=int(input())                                                                               
numberofguesses = 10                                                                                
while catagory > 4 or catagory < 0:                                                                                 
    print('Your input isn\'t one of the catagories. Make sure your choice is a number from 1 to 4.')
    print('Try entering again')                                                                     
    print('Type 1 for Animal, 2 for Clothing, 3 for Weather, 4 for Colors')                         
    catagory=int(input())                                                                           

if catagory == 1: secretword=random.choice(Animal)                           
if catagory == 2: secretword=random.choice(Clothing)                         
if catagory == 3: secretword=random.choice(Weather)                          
if catagory == 4: secretword=random.choice(Colors)                           

hiddenword = ['?' for x in secretword]
hiddenguessed = ''.join(hiddenword)

print('\nThe word you\'re after is ' + hiddenguessed + '\n')                                                                                                   
print('Type one of the following letters, it must be lowercase.')                  
print(Alphabet)                                                                    

i = input()                    

while numberofguesses > 0: 
    if i in secretword:
        if i in Alphabet:
            Alphabet.remove(i)
        print('\nYou guessed correctly')
        hiddenword=[i if i==y else x for x, y in zip(hiddenword, secretword)]
        hiddenguessed= ''.join(hiddenword)
        print('Here is what you have so far '+ hiddenguessed + '\n')
        print('Avalable letters left')
        print(Alphabet)      
        print('Guess another letter:')
        i=input()

    else:
        numberofguesses -= 1
        if i in Alphabet:
            Alphabet.remove(i)
        print("\nYou guessed wrong\n")
        print('Here is what you have so far '+ str(hiddenguessed) + '\n')
        print('You now have '+str(numberofguesses)+' guesses left.')
        print('Type one of the following letters, it must be lowercase.')
        print(Alphabet)
        i=input()
else:
    print('Congrats you have won the game.  ' + secretword + ' was the secret word')

答案 1 :(得分:1)

我建议如下:

首先,创建一个字典,其中包含secretword中的所有字母,其中false为value(需要某种str.lower())

secretdict = {k:False for k in list(secretword)}

然后,如果你受到了打击,你就可以使该值成立。 然后在循环中检查是否为真。

if i in secretword:
    secretdict[i] = True
    hiddenword=[x if secretdict[x] == True else "?" for x in secretword]

答案 2 :(得分:1)

我对功能正常的版本

import random
import string

print('Welcome to hangman')
print('Type one of the following catagories')

categorytypes = {
  "Animal": ['Cat','Dog','Bird','Cow','Fish','Lizard'],
  "Clothing": ['Shirt','Jeans','Sweatshirt','Shoes','Hat','Scarf'],
  "Weather": ['Rain','Snow','Sunny','Sleet','Windy','Stormy'],
  "Colors": ['Red','Blue','Green','Purple','Yellow','Grey']
}

alphabet= list(string.ascii_lowercase)

print('Type Animal, Clothing, Weather, or Colors')

category=input()

if category not in categorytypes.keys():
  print('Your input isn\'t one of the catagories')
  print('Try entering again')
  print('Type Animal, Clothing, Weather, or Colors')
  category=int(input())

secretword = random.choice(categorytypes[category]).lower()

hiddenguessed = ('?'*len(secretword))

gamestate = 0

numberofguesses = 10

print(secretword)
print('')
print('The word you\'re after is ' + hiddenguessed)
print("\n you have {0} guesses".format(numberofguesses))
print('\nType one of the following letters:\n{0}'.format(", ".join(alphabet)))

while gamestate == 0:
  i = input()
  if i in secretword:
    alphabet.remove(i)
    print('You guessed correctly')
    hiddenguessed=[i if secretword[x]==i else hiddenguessed[x] for x in range(len(secretword))]
    hiddenguessed= ''.join(hiddenguessed)
    if hiddenguessed == secretword:
      gamestate = 2
      print("you win")
    else:
      print('Here is what you have so far {0}\nAvalable letters left:\n {1} \nGuess another letter\n'.format(hiddenguessed, ", ".join(alphabet)))

  else:
    alphabet.remove(i)
    print("You guessed wrong")
    print('You now have '+str(numberofguesses)+' guesses left.')
    if numberofguesses != 0:
      print('Here is what you have so far {0}\nAvalable letters left:\n {1} \nGuess another letter\n'.format(hiddenguessed, ", ".join(alphabet)))

  if numberofguesses == 0:
    gamestate = 1

  numberofguesses -= 1

if gamestate == 1:
  print("you lose lol\n")

其他发布的人没有使用:.format,dictionaries和字符串库。