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



我正在制作一个刽子手游戏,作为python的初学者项目。我遇到了一个零件的障碍,这让我发疯了。 我想将单词显示为????当用户猜出正确的字母时,它会在正确的位置用正确的字母替换"?"。

例:

秘密这个词是????

"用户输入 f">

你是对的

这是你到目前为止所拥有的???

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

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())
while catagory > 4:
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())
while 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')
print('')
print('You have ' + str(numberofguesses) + ' left.  Good luck.'
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 = ('?'*len(secretword))
print('')
print('The word you're after is ' + hiddenword)
print('')
print('Type one of the following letters, it must be lowercase.')
print(Alphabet)
i = input()
while numberofguesses != 0:
if i in secretword:
Alphabet.remove(i)
print('')
print('You guessed correctly')
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))
print('')
print('Avalable letters left')
print(Alphabet)      
print('Guess another letter')
i=input()
else:
numberofguesses = numberofguesses - 1
Alphabet.remove(i)
print('')
print("You guessed wrong")
print("")
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))
print('')
print('You now have '+str(numberofguesses)+' guesses left.')
print('Here is what you have so far '+ str(hiddenguessed))
print('')
print('Type one of the following letters, it must be lowercase.')
print(Alphabet)
i=input()
else:
Print('Congrats you have won the game.  ' + _ 
str(hiddenguessed) + ' was the secret word')    

我试图用字符串列出一个列表,然后翻转字符,但我没有任何运气。

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))

这是我能做的最好的事情,但它所做的只是用猜测的字母替换所有的"?"。虽然它只有在猜对的字母时才这样做有什么想法吗?

我对功能版本的看法

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 lettern'.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 lettern'.format(hiddenguessed, ", ".join(alphabet)))
if numberofguesses == 0:
gamestate = 1
numberofguesses -= 1
if gamestate == 1:
print("you lose loln")

其他发布的人不使用一些很酷的东西:.format,字典和字符串库。

我建议如下:

首先,创建一个字典,保存密码中的所有字母,并以 false 作为值(虽然需要某种 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]

这个答案不包含所有修复的错误 - 只是让它运行的最小数量。

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 wrongn")
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')

最新更新