Я никогда по-настоящему не делал игру или что-то подобное на питоне, но мне было скучно, поэтому я попытался сделать игру в виде палача. Мне не нравится, сколько операторов if else я использовал, но я не могу придумать лучшего способа сделать это. Любые отзывы приветствуются!
# global varibles
SECRET_WORD = "Python".lower()
#main function
def main():
#setups Variable
CHAR_WORD = set(SECRET_WORD)
NUMBER_OF_GUESSES = 6
CORRECT_GUESSES = set()
WRONG_GUESSES = set()
# while we still have guess
while NUMBER_OF_GUESSES > 0:
# gets our next guess
current_guess = input("Guess: ").lower()
# checks if our guess is only 1 char long
if len(current_guess) != 1:
print("Please Input a Character")
# checks if we have already guessed the character
elif current_guess in CORRECT_GUESSES or current_guess in WRONG_GUESSES:
print("Already Guess that Character")
else:
#if our guess is in the word we add the the char to the correct guesses set
if current_guess in CHAR_WORD:
CORRECT_GUESSES.add(current_guess)
print(f"Correct, {NUMBER_OF_GUESSES} guesses remaining. Current Word: {blank_letters(CORRECT_GUESSES, SECRET_WORD)}n")
else:
WRONG_GUESSES.add(current_guess)
NUMBER_OF_GUESSES -= 1
print(f"Wrong, {NUMBER_OF_GUESSES} guesses remaining. Current Word: {blank_letters(CORRECT_GUESSES, SECRET_WORD)}n")
if CORRECT_GUESSES == CHAR_WORD:
break
# if we break out of the loop and have more than 1 guess we win
if NUMBER_OF_GUESSES != 0:
print("Congratulations, You Won")
#if we break out the loop and have 0 guess we have lost
else:
print(f"You Lost, The Word Was: {SECRET_WORD}")
#used to return a string only showing the letters we have already guessed
def blank_letters(current_Guesses, secret_Word):
return_sting = ""
#loops through all the letters
for i in range(len(secret_Word)):
# if we havent guess the current letter we replace that letter with an underscore
if secret_Word[i] not in current_Guesses:
return_sting += '_'
# if we have guessed the current word we replace it with that letter
else:
return_sting += secret_Word[i]
return return_sting
if __name__ == '__main__':
print("n")
main()