【发布时间】:2017-10-14 09:29:58
【问题描述】:
基本上我正在尝试制作一个刽子手类型的游戏。当猜到一个字母时,我希望该字母在下一轮中保留在那里。我试着用一个列表来做这个,但我不知道如何让它用字母替换特定的空白。
是的,我已阅读有关此主题的其他问题,但无法让它们为我工作。
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()
【问题讨论】:
-
您的代码中究竟有什么问题?或者至少你认为它在哪里有问题?
-
您究竟想存储什么?随机词,还是用户尝试?
标签: python