基本的に私はハングマンタイプのゲームを作ろうとしています。言葉にある手紙が推測されるとき、私はその手紙が次のラウンドでそこにとどまることを望みます。私はリストを使ってこれをやろうとしましたが、特定の空白を手紙に置き換える方法を見つけることができませんでした。Pythonの推測ゲームは、空白を文字で置き換えてリストに格納します。
はい、私はこのトピックに関する他の質問を読みましたが、私のために働かせることができませんでした。それを行うには、よりpytonishな方法があります
randWord = "something"
blanks = len("something")*"_ "
guessLetter = "t"
print(blanks)
if guessLetter in randWord:
for index, letter in enumerate(randWord, 0):
if guessLetter == randWord[index]:
temp = list(blanks)
temp[2*index] = guessLetter
blanks = "".join(temp)
print(blanks)
が、あなたのコードに判断すると、私はあなたにそれを表示しないことを好む:
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()
として考慮されていないことを確認したいと思うかもしれません:あなたは、ループしながら、次のように使用していることを修正することができますあなたのコードで?または少なくともそれは問題があると思いますか? –
正確に何を保存したいですか?ランダムな単語、またはユーザーの試みですか? – Ajax1234