MegoTech / Code-questions-marathon

***A package of code questions***
4 stars 24 forks source link

weekly quiz no 5 #10

Open moshe-h-m opened 1 year ago

moshe-h-m commented 1 year ago

5. Complex task: Write a program that simulates a simple game of hangman. The program should randomly choose a word from a list of words and display the word with underscores in place of the letters. The user should then be prompted to guess a letter. If the letter is in the word, the program should replace the corresponding underscore with the letter. If the letter is not in the word, the program should subtract a life (the user starts with 5 lives) and display the updated state of the player. The game should continue until the user correctly guesses the word or runs out of lives. Use functions to organize your code.

the file to implement is here: click me~

Example input/output: Possible words: ["apple", "banana", "orange"] The word is: Guess a letter: a The word is: a Guess a letter: r The word is: a p p Guess a letter: p The word is: a p p Guess a letter: l The word is: a p p l Guess a letter: e Congratulations, you guessed the word! Tip!! divied this program to some functions

moshe-h-m commented 1 year ago

NOTE!! To create sign function with the post fix with your name like this:

def #func_your_name(): pass

YankiBaras commented 1 year ago

import random

def choose_word(lst): return lst[random.randint(0, len(lst) - 1)]

def show_hidden_word(secret_word, old_letters_guessed): new_list = list(secret_word) for k in range(len(secret_word)): if secret_word[k] not in old_letters_guessed: newlist[k] = "" return " ".join(new_list)

def check_win(secret_word, old_letters_guessed): new_list = list(secret_word) for j in range(len(secret_word)): if new_list[j] not in old_letters_guessed: return False return True

list1 = ["apple", "banana", "orange"] guessed = [] i = 5 word_to_guess = choose_word(list1) print("The word is: ", show_hidden_word(word_to_guess, guessed)) while i > 0: letter = input("Guess a letter: ") guessed.append(letter) if letter not in word_to_guess: i -= 1 print("The word is: ", show_hidden_word(word_to_guess, guessed)) if check_win(word_to_guess, guessed) is True: print("Congratulations, you guessed the word!") break