gpt4 book ai didi

Python猜谜游戏,用字母替换空格存入列表

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:06 26 4
gpt4 key购买 nike

基本上我正在尝试制作刽子手类型的游戏。当猜到单词中的字母时,我希望该字母在下一轮中留在那里。我试着用一个列表来做这件事,但我不知道如何让它用字母替换特定的空白。

是的,我已经阅读了有关该主题的其他问题,但无法让它们为我工作。

import random
import time

def intro():
print ('''Welcome to the word guessing game!
You will have 6 letter guesses to figure out my word!
First you must choose a topic.
Choose from the following: (1) Cars , (2) Diseases ,
(3) Animals , or (4) Mathematical Words''')

def optionSelect():
choice = ''
while choice != '1' and choice != '2' and choice !='3' and choice !='4':
choice = input()
return choice

def checkChoice(chosenOption):
if chosenOption == '1':
sectionOne()
elif chosenOption == '2':
sectionTwo()
elif chosenOption == '3':
sectionThree()
elif chosenOption == '4':
sectionFour()
else:
print('You didnt choose an option...')

def sectionOne():
words = ['mclaren ', 'bugatti ', 'aston martin ', 'mitsubishi ']
randWord = random.choice(words)
blanks = '_ ' * len(randWord)
guessing(blanks, randWord)


def sectionTwo():
words = ['gonorrhea ', 'nasopharyngeal carcinoma ', 'ependymoma ', 'tuberculosis ']
randWord = random.choice(words)
blanks = '_ ' * len(randWord)
guessing(blanks, randWord)



def sectionThree():
words = ['tardigrade ', 'komodo dragon ', 'bontebok ', 'ferruginous hawk ']
randWord = random.choice(words)
blanks = '_ ' * len(randWord)
guessing(blanks, randWord)


def sectionFour():
words = ['pythagorean ', 'differentiation ', 'polyhedron ', 'googolplex ']
randWord = random.choice(words)
blanks = '_ ' * len(randWord)
guessing(blanks, randWord)


def guessing(blanks, randWord):
missed = []
print ()
print ("Word: ",blanks)
attempts = 15
while attempts != 0:
attempts = attempts - 1
print ('Guess a letter, you have ' + str(attempts) + ' attempts left.')
guessLetter = input()
if guessLetter in randWord:
newBlanks = " ".join(c if c in guessLetter else "_" for c in randWord)
print ('Correct!')
print ()
print ("Word: ",newBlanks)
else:
missed.append(guessLetter)
print ('That letter is not in the word, you have guessed the following letters:')
print (', '.join(missed))
print ()


playAgain = ''
while playAgain != 'yes' and playAgain!= 'no':
intro()
optionNumber = optionSelect()
checkChoice(optionNumber)
print('Do you want to play again? (yes or no)')
#Play again or not
playAgain = input()
if playAgain == 'yes':
played = 1
playAgain =''
else:
print('Thanks for Playing! Bye!')
time.sleep(2)
quit()

最佳答案

问题是您没有存储 newBlanks,因此每次用户猜对字母时,程序都会重新创建字符串。因此,您需要跟踪猜对了哪些字母。您可以通过替换 if 语句中未重估的局部变量中的每个猜测字母来实现。您可以使用以下 while 循环解决此问题:

while attempts != 0:
attempts = attempts - 1
print ('Guess a letter, you have ' + str(attempts) + ' attempts left.')
guessLetter = input()
if guessLetter in randWord:
newBlanks = " ".join(c if c in guessLetter else "_" for c in randWord)
index = 0
for letter in newBlanks:
if letter != '_' and letter != ' ':
blanks= blanks[:index] + letter + blanks[index+1:]
index += 1
print ('Correct!')
print ()
print ("Word: ",blanks)

ps,你的一些单词中有空格,你可能想确保空格不被视为猜测

关于Python猜谜游戏,用字母替换空格存入列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43982155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com