Я почти закончил кодировать свою игру в палач в Trinket, используя Python, и я решил добавить визуальный аспект, чтобы он не был таким скучным. Вскоре я столкнулся с проблемой, заключающейся в том, что, когда я добавляю 1 в переменную 'fail', она будет удерживать ее значение до тех пор, пока не будет повторен 'for l oop'. Я не добавил список слов, потому что он занял бы здесь слишком много места и не имеет отношения к делу. Вот мой код целиком:
# Prints the visual scaffold
def print_scaffold(guessed, fail):
if fail == 0:
print ""
print ""
print ""
print ""
print ""
print ""
print "________"
elif fail == 1:
print ""
print ""
print ""
print ""
print ""
print ""
print "|________"
elif fail == 2:
print ""
print ""
print ""
print ""
print ""
print "|"
print "|________"
elif fail == 3:
print ""
print ""
print ""
print ""
print "|"
print "|"
print "|________"
elif fail == 4:
print ""
print ""
print ""
print "|"
print "|"
print "|"
print "|________"
elif fail == 5:
print ""
print ""
print "|"
print "|"
print "|"
print "|"
print "|________"
elif fail == 6:
print ""
print "|"
print "|"
print "|"
print "|"
print "|"
print "|________"
elif fail == 7:
print "_________"
print "|"
print "|"
print "|"
print "|"
print "|"
print "|________"
elif fail == 8:
print "_________"
print "| |"
print "|"
print "|"
print "|"
print "|"
print "|________"
elif fail == 9:
print "_________"
print "| |"
print "| O"
print "|"
print "|"
print "|"
print "|________"
elif fail == 10:
print "_________"
print "| |"
print "| O"
print "| |"
print "|"
print "|"
print "|________"
elif fail == 11:
print "_________"
print "| |"
print "| O"
print "| \|"
print "|"
print "|"
print "|________"
elif fail == 12:
print "_________"
print "| |"
print "| O"
print "| \|/"
print "|"
print "|"
print "|________"
elif fail == 13:
print "_________"
print "| |"
print "| O"
print "| \|/"
print "| |"
print "|"
print "|________"
elif fail == 14:
print "_________"
print "| |"
print "| O"
print "| \|/"
print "| |"
print "| /"
print "|________"
elif fail == 15:
print "_________"
print "| |"
print "| O"
print "| \|/"
print "| |"
print "| / \ "
print "|________ "
guessed = True
return guessed, fail;
# Tests whether or not your guess was correct
def check(word, guesses, guess, fail):
status = ''
matches = 0
for letter in word:
if letter in guesses:
status += letter
else:
status += ' _ '
if letter == guess:
matches += 1
if matches > 1:
print('Yes! The word contains ' + str(matches) + ' "' + guess + '"' + 's.')
elif matches == 1:
print('Yes! The word contains the letter "' + guess + '".')
else:
print('Sorry. The word does not contain the letter "' + guess + '".')
fail = fail + 1
print(status)
print(fail) # I put this here to see if the varaible held it's value after the 'IF' statement
return status, fail;
# Main part of the game; this is what runs
def main():
# Chooses a random word from a list of 200
word = random.choice(dictionary).upper()
guesses = []
fail = 0
guessed = False
print("The word contains " + str(len(word)) + " letters.")
while not guessed:
text = '\nPlease enter one letter or a {}-letter word. '.format(len(word))
print(fail) # Shows if the varaible held it's value entering the repeat
print_scaffold(guessed, fail)
print(fail) # Shows if the varaible held it's value after the function
guess = input(text)
guess = guess.upper()
if guess in guesses:
print('You already guessed "' + guess + '".')
elif len(guess) == len(word):
guesses.append(guess)
if guess == word:
guessed = True
else:
print("Sorry, that is incorrect.")
elif len(guess) == 1:
guesses.append(guess)
print(fail) # Shows if the varaible held it's value before it was updated
result = check(word, guesses, guess, fail)
if result == word:
guessed = True
else:
print("Invalid entry.")
print("\nYes, the word is "+ word + "! Good Job!")
main()
Это то, что выводится, если вы введете неправильную букву. К вашему сведению, когда игрок отвечает на письмо правильно, он заменяет соответствующее подчеркивание на букву. Числа показывают значение переменной 'fail'. 1 перед повторением, но 0 после него является значением переменной 'fail' после повторения.
The word contains 8 letters.
0
________
0
Please enter one letter or a 8-letter word. a
0
Sorry. The word does not contain the letter "A".
_ _ _ _ _ _ _ _
1
0
________
0
Please enter one letter or a 8-letter word.