コンピューティングの評価のためにハングマン ゲームをプログラミングしようとしてきましたが、ちょっとした障害にぶつかりました。
基本的に、プログラムはユーザーに単語を要求し、ループを実行して、入力された単語と同じ長さのアスタリスクの文字列を作成します。
ユーザーが正しい文字の 1 つを入力すると、アスタリスクが正しい文字に置き換えられますが、単語の順序で置き換えられます。たとえば、単語が「嘘」で、ユーザーが「i」と入力すると、「*」が「i」に変更されます。
以下のようにコードします。
def guess_part(word):
lives = 6
LetterCount = 0
LetterMask = ""
for x in range(len(word)):
LetterMask = LetterMask + "*"
print LetterMask
while lives != 0 and LetterMask.find("*")!=-1:
LetterGuess = raw_input("Enter a letter to guess?")
LetterCount = 0
for char in word:
LetterCount = LetterCount + 1
if LetterGuess == char:
print "Good Guess."
LetterMask = LetterMask.replace(LetterMask[LetterCount], LetterGuess)
print LetterMask
def rand_word():
from random import randrange #import the randrange function, from "random"
random_words = ['extraordinary','happy','computer','python','screen','cheese','cabaret','caravan','bee','wasp','insect','mitosis','electronegativity','jumper','trousers'] #list of different words which can be used by the program for the end user to guess.
word = random_words[randrange(0, 15)] #pick a random number, and use this number as an index for the list, "random_words".
guess_part(word) #call the function, "guess_part" with the parameter "word"
def user_word():
print "All words will be changed to lowercase."
print "Enter the word you would like to guess."
print ""
validation_input = False #Setting the validation unput to "False"
while validation_input == False: #while the validation input is not False, do below.
word = raw_input("") #Ask for input, and set the value to the variable, "word".
if word.isalpha(): #If word contains only strings, no numbers or symbols, do below.
word = word.lower() #set the string of variable, "word", to all lowercase letters.
guess_part(word) #call the function, "guess_part" with the parameter, "word".
validation_input = True #Break the while loop - set validation_input to "False".
else: #if the above isn't met, do the below.
print "Word either contained numbers or symbols."
def menu():
print "Hangman Game"
print ""
print "Ashley Collinge"
print ""
print "You will have 6 lives. Everytime you incorrectly guess a word, you will lose a life."
print "The score at the end of the game, is used to determine the winner."
print ""
print "Would you like to use a randomly generated word, or input your own?"
print "Enter 'R' for randomly generated word, or 'I' for your own input."
decision_bool = False #Set the decision_bool to "False".
decision_length = False #Set the decision_length to "False".
while decision_bool == False: #While decision_bool equals "False", do below.
decision = raw_input("") #Ask for input, value set to the variable "decision".
while decision_length == False: #While decision_length equals "False", do below.
if len(decision) == 1: #If the length of decision eqausl 1, do below.
decision_length = True #Set decision_length to "True."
decision = decision.capitalize() #Capitalize the string value of decision.
if decision == "R": #if the value of decision, eqauls "R".
print "You chose randomly generated word."
print ""
print "Forwarding..."
decision_bool = True #Set decision_bool to "True".
print ""
rand_word() #Call the function, rand_word()
elif decision =="I": #If decision equals "I", do below.
print "You chose to input your own word."
print ""
print "Forwarding..."
decision_bool = True #Set decision_bool to "False".
print ""
user_word() #Call the function, user_word()
else:
print "You entered an incorrect value for the question. Try again."
else:
print "You entered an incorrect value for the question. Try again."
menu()
コードの大部分をコメントしましたが、少しあいまいなことがあればお答えします。