From 764c863f854c4e6eba74f16f9ccef438eec1eb53 Mon Sep 17 00:00:00 2001 From: Ankit Date: Thu, 20 May 2021 16:03:11 +0530 Subject: [PATCH] first commit --- __pycache__/images.cpython-39.pyc | Bin 0 -> 811 bytes __pycache__/words.cpython-39.pyc | Bin 0 -> 850 bytes hangman.py | 94 ++++++++++++++++++++++-------- 3 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 __pycache__/images.cpython-39.pyc create mode 100644 __pycache__/words.cpython-39.pyc diff --git a/__pycache__/images.cpython-39.pyc b/__pycache__/images.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..f2ba1ebfe2348f7f089adca98c90b8adb94b0171 GIT binary patch literal 811 zcmYe~<>g`kg3hd^2?ERv439w^WWWUEH~?`m50FS_NMTsS7{!pn7|fu_q{&(3&&8zx z1lqc~y4qk?4Uks@W`V?sW!b_2J_8MCW}rS{x56A~OOy-s2?qnI2F4HyO|lKdE)K2m@KTxH!4|G#PKPdHOoKy9TdhC;}y1F!9Sc#VW=nwX8TVwYWqfpeR2p yHMyiX#v?H=JvT8gCIdq2mH=gAGIJBtQ;YQqDsOSvw3asj literal 0 HcmV?d00001 diff --git a/__pycache__/words.cpython-39.pyc b/__pycache__/words.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..aabd30a0d5cee3442f7becf44ed38c1f22f02e76 GIT binary patch literal 850 zcmb_ay>1gh5Z=8zo3leI3L1*hRX!=9ph2<#Awi^62#M}63dimGe93y(+Fi#uvMYEF zlu#h?O4m~H3eYgKheOgc($3%Qd^<0xmCBoo29`^v1V1g@F zFcFFPGb^}AL=SBw_#2k@f8q`Hsv^A;jt(Kbz~kdCCL zhaSI9sN6kVtlJ@N^(2G|aYBmSNe8xv_7-*YPK+l|N923ESQ^~fc%u3okveb0D=VXO=VbXsqvovnIhZV-j@a;Y(T#AbI`h3V!ZU^Y+ gvF)P?4~4`Hh_L^FlRlXnu~hOI{pDekX`J5s1N{cq5C8xG literal 0 HcmV?d00001 diff --git a/hangman.py b/hangman.py index 7bc94b7..87e3f77 100644 --- a/hangman.py +++ b/hangman.py @@ -8,14 +8,11 @@ ''' def is_word_guessed(secret_word, letters_guessed): - ''' - secret_word: word guess by the user - letters_guessed: list hold all the word guess by the user - returns: - return True (if user guess the world correctly ) - return False (wrong selection) - ''' - return False + n = len(secret_word) + for i in range(n): + if secret_word[i] not in letters_guessed: + return False + return True # if you want to test this function please call function -> get_guessed_word("kindness", [k, n, d]) @@ -51,7 +48,31 @@ def get_available_letters(letters_guessed): return sting is -> `bcdfghijklmnopqrstuvwxyz` ''' letters_left = string.ascii_lowercase - return letters_left + word = "" + for ch in letters_left: + if ch in letters_guessed: + continue + else: + word += "" + + return word + + +def is_valid(letter): + if len(letter) == 1: + chars = string.ascii_lowercase + if letter in chars: + return True + return False + + +def hang_the_man(count): + print(IMAGES[count - 1]) + +def give_hint(letters_guessed, secret_word): + for ch in letters_guessed: + if ch not in secret_word: + print("Hint: {}".format(ch)) def hangman(secret_word): @@ -70,28 +91,55 @@ def hangman(secret_word): * Display partial word guessed by the user and use underscore in place of not guess word ''' print("Welcome to the game, Hangman!") + print("I am thinking of a word that is {} letters long.".format( str(len(secret_word))), end='\n\n') letters_guessed = [] + flag = True + remaining_lives = 8 + count = 0 available_letters = get_available_letters(letters_guessed) print("Available letters: {} ".format(available_letters)) - guess = input("Please guess a letter: ") - letter = guess.lower() - - if letter in secret_word: - letters_guessed.append(letter) - print("Good guess: {} ".format( - get_guessed_word(secret_word, letters_guessed))) - if is_word_guessed(secret_word, letters_guessed) == True: - print(" * * Congratulations, you won! * * ", end='\n\n') - else: - print("Oops! That letter is not in my word: {} ".format( - get_guessed_word(secret_word, letters_guessed))) - letters_guessed.append(letter) - print("") + while remaining_lives > 0: + + # available_letters = get_available_letters(letters_guessed) + # print("Available letters: {} ".format(available_letters)) + + guess = input("Please guess a letter: ") + letter = guess.lower() + + if len(letter) == 4 and letter == "hint" and flag: + give_hint(letters_guessed, secret_word) + flag = False + + if is_valid(letter): + remaining_lives -= 1 + count += 1 + + hang_the_man(count) + + print(remaining_lives) + + if letter in secret_word: + letters_guessed.append(letter) + print("Good guess: {} ".format(get_guessed_word(secret_word, letters_guessed))) + + if is_word_guessed(secret_word, letters_guessed) == True: + print(" * * Congratulations, you won! * * ", end='\n\n') + break + else: + print("Oops! That letter is not in my word: {} ".format( + get_guessed_word(secret_word, letters_guessed))) + letters_guessed.append(letter) + print("") + + + if remaining_lives == 0: + print("You lost", end = '\n') + hang_the_man(count) # Load the list of words into the variable wordlist