Игра в Hangman – это всего лишь слово «угадывая игру», угадая персонажа слова. В этой игре есть список присутствующих слов, из которого наш интерпретатор выберет 1 случайное слово.
Пользователь сначала должен ввести свои имена, а затем попросить угадать любой алфавит. Если случайное слово содержит этот алфавит, оно будет показано как выход (с правильным размещением), иначе программа попросит вас угадать другой алфавит. Пользователям будет дано 12 поворотов (может быть изменено соответствующим образом), чтобы угадать полное слово.
# library that we use in order to choose # on random words from a list of words import random # Here the user is asked to enter the name first name = input("Enter your name: ") print("All the best", name) words = [ "website", "hangman", "rainbow", "computer", "science", "programming", "python", "mathematics", "player", "apple", "reverse", "water", "binod", "codesnail", ] # Function will choose one random # word from this list of words word = random.choice(words) print("\nGuess the characters") guesses = "" # any number of turns can be used here turns = 12 while turns > 0: # counts the number of times a user fails failed = 0 # all characters from the input # word taking one at a time. for char in word: 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("\n\nYou Win") # this print the correct word print("\nThe word is: ", word) break # if user has input the wrong alphabet then # it will ask user to enter another alphabet guess = input("\n\nguess the character: ") # 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 the character doesn't match the word # then "Wrong" will be given as output print("\nWrong") # this will print the number of # turns left for the user print("\nYou have", +turns, "more guesses") if turns == 0: print("\n\nYou lose")
Оригинал: Игра для палачков в Python
Больше проектов Python 👇
- Tic Tac Toe Game в Python Amazing Mini Project
- Угадайте число номеров в Python
- Rock Paper Scissors игра в Python
Оригинал: “https://dev.to/soniarpit/hangman-game-in-python-2cl5”