#python #string #function #loops #methods
#python #строка #функция #циклы #методы
Вопрос:
Я пишу код для игры в word puzzle и пытаюсь добавить строку «Ответ до сих пор» перед «_ _ _ _ _», как на выходном изображении ниже
import random
import re
отобразить инструкции
infile = open('wp_instructions.txt', 'r')
line = infile.readline()
while line:
print(line.strip()) #prints line
line = infile.readline() #loops the iteration
infile.close()
words = ['apple', 'banana', 'watermelon', 'kiwi', 'pineapple', 'mango']
word = random.choice(words)
symbol = "_"
blanklines = len(word) * symbol
status = "The answer so far is "
print(status, end=" ")
guesses = ''
здесь можно использовать любое количество ходов
turns = 4
while turns > 0:
подсчитывает количество сбоев пользователя
failed = 0
все символы из ввода.
слово берется по одному.
для символа в word:
# comparing that character with
# the character in guesses
if char in guesses:
print(char, end= ' ')
else:
print("_", end= ' ')
# for every failure 1 will be
# incremented in failure
failed = 1
if failed == 0:
# user will win the game if failure is 0
# and 'You Win' will be given as output
print("Good job! You found the word", word '!')
endgame_button = input('Press enter to end the game.')
if endgame_button == '':
break
# if user has input the wrong alphabet then
# it will ask user to enter another alphabet
guess = input("nGuess a letter (" str(turns) " " "guesses remaining): ")
# every input character will be stored in guesses
guesses = guess
# check input with the character in word
if guess not in word:
turns -= 1
if turns == 0:
print('Not quite, the correct word was ' str(word) '. Better luck next time')
endgame_button = input('Press enter to end the game.')
if endgame_button == '':
break
Комментарии:
1. Не ясно, что вам нужно .. постарайтесь свести к минимуму свой вопрос или объяснить его лучше (мое предложение с более короткими описаниями)
2. @adirabargil Я отредактировал его и надеюсь, что теперь это имеет смысл