From 1694bf50c149a85b2f109b907c36a2c998026372 Mon Sep 17 00:00:00 2001 From: Liron-Mizrahi <91504420+lironmiz@users.noreply.github.com> Date: Thu, 5 Jan 2023 07:47:28 +0200 Subject: [PATCH 1/8] Create hangmanproject_unit9.py --- hangmanproject_unit9.py | 50 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 hangmanproject_unit9.py diff --git a/hangmanproject_unit9.py b/hangmanproject_unit9.py new file mode 100644 index 0000000..393c9bf --- /dev/null +++ b/hangmanproject_unit9.py @@ -0,0 +1,50 @@ +# hangman project - unit 9 exercise. +# author - lirom mizrahi +''' +This task will choose for the player a word that will be the secret word for the guess, +from a text file containing a list of words separated by spaces. + +Write a function called choose_word defined as follows: + +def choose_word(file_path, index): +The function accepts as parameters: + +A string (file_path) representing a path to the text file. +An integer (index) representing the position of a certain word in the file. +The function returns a tuple consisting of two members in the following order: + +The number of different words in the file, i.e. not including repeated words. +A word in the position received as an argument to the function (index), which will be used as the secret word for guessing. +Guidelines +Treat the positions the player enters as starting from 1 (rather than zero). +If the position (index) is greater than the number of words in the file, the function +continues to count positions in a circular fashion (that is, returns to the first position +in the original list of words in the file and God forbid). +An example of a text file that contains a list of words, called words.txt +hangman song most broadly is a song hangman work music work broadly is typically +Examples of running the choose_word function with the words.txt file +>>> choose_word(r"c:\words.txt", 3) +(9, 'most') +>>> choose_word(r"c:\words.txt", 15) +(9, 'hangman') + +''' + +def choose_word(file_path, index): + with open(file_path, 'r') as f: + words = f.read().split() + + num_unique_words = len(set(words)) + + secret_word = words[(index - 1) % len(words)] + + return (num_unique_words, secret_word) + +def main(): + result = choose_word(r"words.txt", 3) + print(result) + result = choose_word(r"words.txt", 15) + print(result) + +if __name__ == "__main__": + main() From 16993d34d0a2ed7d3a0b551999034115977b48f7 Mon Sep 17 00:00:00 2001 From: Liron-Mizrahi <91504420+lironmiz@users.noreply.github.com> Date: Thu, 5 Jan 2023 09:56:33 +0200 Subject: [PATCH 2/8] Update hangmanproject_unit9.py --- hangmanproject_unit9.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hangmanproject_unit9.py b/hangmanproject_unit9.py index 393c9bf..0f08c2c 100644 --- a/hangmanproject_unit9.py +++ b/hangmanproject_unit9.py @@ -36,7 +36,7 @@ def choose_word(file_path, index): num_unique_words = len(set(words)) - secret_word = words[(index - 1) % len(words)] + secret_word = words[(int(index) - 1) % len(words)] return (num_unique_words, secret_word) From d9f5f6f0c2969cda3bf5a1f929dc7786ae9ee699 Mon Sep 17 00:00:00 2001 From: Liron-Mizrahi <91504420+lironmiz@users.noreply.github.com> Date: Thu, 5 Jan 2023 11:50:20 +0200 Subject: [PATCH 3/8] add the final project --- hangman_final_project.py | 225 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 hangman_final_project.py diff --git a/hangman_final_project.py b/hangman_final_project.py new file mode 100644 index 0000000..e8cf252 --- /dev/null +++ b/hangman_final_project.py @@ -0,0 +1,225 @@ +import sys +def welcome_Screen(): + """ + Print the logo and number of attempts. + + :param None + :return: None + :rtype: None + """ + + HANGMAN_GAME_LOGO = """ + __ __ ___ .__ __. _______ .___ ___. ___ .__ __. _______ ___ .___ ___. _______ + | | | | / \ | \ | | / _____|| \/ | / \ | \ | | / _____| / \ | \/ | | ____| + | |__| | / ^ \ | \| | | | __ | \ / | / ^ \ | \| | | | __ / ^ \ | \ / | | |__ + | __ | / /_\ \ | . ` | | | |_ | | |\/| | / /_\ \ | . ` | | | |_ | / /_\ \ | |\/| | | __| + | | | | / _____ \ | |\ | | |__| | | | | | / _____ \ | |\ | | |__| | / _____ \ | | | | | |____ + |__| |__| /__/ \__\ |__| \__| \______| |__| |__| /__/ \__\ |__| \__| \______| /__/ \__\ |__| |__| |_______| + """ + print(HANGMAN_GAME_LOGO) + MAX_TRIES = 6 + print(f"you have {MAX_TRIES} guess") + +def choose_word(file_path, index): + """ + the function return the secret word for guessing. + + :param file_path: A string representing a path to a text file containing space-separated words + :param index: Position of a particular word in the file + :return: the secret word for guessing + :rtype: str + """ + # open the file at read mode and read the words from the file + while True: # check if user file_path correct + try: + with open(file_path, 'r') as file: + words = file.read().split() + # take the secret word + while True: # check if user index is correct + try: + secret_word = words[(int(index) - 1) % len(words)] + return secret_word + except ValueError: + print(f"sorry, {index} is not a corret index. Please try again!") + sys.exit() + + except: + print(f"sorry, {file_path} is not a corret file name. Please try again!") + sys.exit() + +def print_hangman(num_of_tries): + """Prints the value of the key given as paramter. + + :param num_of_tries: a key for the dict(HANGMAN_PHOTOS) + :type num_of_tries: int + :return: None + """ + + HANGMAN_PHOTOS = { + 1: """ x-------x""", + 2: """ x-------x + | + | + | + | + |""", + 3: """ x-------x + | | + | 0 + | + | + |""", + 4: """ x-------x + | | + | 0 + | | + | + |""", + 5: """ x-------x + | | + | 0 + | /|\\ + | + |""", + 6: """ x-------x + | | + | 0 + | /|\\ + | / + |""", + 7: """ x-------x + | | + | 0 + | /|\\ + | / \\ + |""" + } + hangman_status = HANGMAN_PHOTOS[num_of_tries] + print(hangman_status) + +def check_valid_input(letter_guessed, old_letters_guessed): + """ + A Boolean function that accepts a character and a list of letters + that the user has guessed previously. The function checks + two things: the correctness of the input and whether it is legal + to guess this letter (that is, the player has not guessed this letter before) + and returns true or false accordingly. + + :param letter_guessed: character received from the user. + :type letter_guessed: str + :param old_letters_guessed: list contains the letters the player has guessed. + :type old_letters_guessed: list + :return: return True if it is a legal input, else return False + :rtype: bool + """ + # if the character is not valid return false + if letter_guessed.isalpha() != True or len(letter_guessed) > 1: + return False + elif letter_guessed in old_letters_guessed: # if the character is not in the letters the player has guessed we return false + return False + else: + return True + +def try_update_letter_guessed(letter_guessed, old_letters_guessed): + """ + The method checks if the character is correct + and if it is correct and does not exist in + the letters that the user guessed then it updates the list with + the new character and returns true otherwise it returns false + + :param letter_guessed: character received from the user. + :type letter_guessed: str + :param old_letters_guessed: list contains the letters the player has guessed. + :type old_letters_guessed: list + :return: return True if it is a legal input, else return False + :rtype: bool + """ + # check if the input is valid + if check_valid_input(letter_guessed, old_letters_guessed) == False: + print('X') + old_letters_guessed = ' -> '.join(sorted(old_letters_guessed)) + print(old_letters_guessed) + return False + else: + old_letters_guessed.append(letter_guessed) + return True + +def show_hidden_word(secret_word, old_letters_guessed): + """ + A function that returns a string consisting of letters and underscores. + + :param secret_word: word the user has to guess + :type secret_word: str + :param old_letters_guessed: list contains the letters the player has guessed. + :type old_letters_guessed: str + :return: reveal the secret word to the player in a lower-line structure + :rtype: str + """ + # we making the result string by loop all the letters in the secret word + # and if the letter in the letters the user has guessed we add the letter to the result + # and if not we and _ to the result + result = '' + for letter in secret_word: + if letter in old_letters_guessed: + result += letter + ' ' + else: + result += '_ ' + return result + +def check_win(secret_word, old_letters_guessed): + """ + This is a boolean function that returns true if all the letters that make up the secret word + are included in the list of letters that the user guessed. Otherwise, the function returns false. + + :param secret_word: word the user has to guess + :param old_letters_guessed: list contains the letters the player has guessed. + :type secret_word: str + :type old_letters_guessed: list + :return: true if all the letters that make up the secret word are included int the list of old_letters_guessed otherwise return false + :rtype: bool + """ + # looping all the letters in secret word if one of them not int the guessed word we return False otherwise we return True + for letter in secret_word: + if letter not in old_letters_guessed: + return False + return True + +def main(): + + # print the welcome screen + welcome_Screen() + wrong_guess = 0 + num_of_tries = 1 + old_letters_guessed = list() # create an empty list for the beginning + # take from user the file path and index + file_path = input("Enter file path: ") + index = input("Enter index: ") + secret_word = choose_word(file_path, index) + print("Let’s start!") + print_hangman(num_of_tries) + print("_ " * len(secret_word)) + # game loop + while wrong_guess in range(6): + # take user input + guessed_letter = input("Guess a letter: ").lower() + # the main logic of the game + if try_update_letter_guessed(guessed_letter, old_letters_guessed): + if guessed_letter not in secret_word: + wrong_guess += 1 + num_of_tries += 1 + print(":(") + print_hangman(num_of_tries) + print(show_hidden_word(secret_word, old_letters_guessed)) + else: + print(show_hidden_word(secret_word, old_letters_guessed)) + if check_win(secret_word, old_letters_guessed): + print("WIN") + break + if wrong_guess == 6: + if check_win(secret_word, old_letters_guessed): + print("WIN") + else: + print("LOSE") + +if __name__ == "__main__": + main() \ No newline at end of file From aa813498b0b7bebbdb856fb2bb4f591271f5227e Mon Sep 17 00:00:00 2001 From: Liron-Mizrahi <91504420+lironmiz@users.noreply.github.com> Date: Thu, 5 Jan 2023 12:31:19 +0200 Subject: [PATCH 4/8] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index b853b24..12b3372 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,12 @@ this repository contains solutions in python to the python course problems of th 9. Files 10. Final project from scratch hangman game. +# video of the final project πŸ€™ + + +https://user-images.githubusercontent.com/91504420/210758896-10ba6b90-9aa2-49ca-8f73-7fdf5c3ed5b0.mp4 + + From 3e44ba3183e58f0da409420211a86069868f8212 Mon Sep 17 00:00:00 2001 From: Liron-Mizrahi <91504420+lironmiz@users.noreply.github.com> Date: Tue, 17 Jan 2023 19:38:30 +0200 Subject: [PATCH 5/8] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 12b3372..eef80c5 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,9 @@ this repository contains solutions in python to the python course problems of th

+![image](https://user-images.githubusercontent.com/91504420/212971858-3c5505ad-1228-4d45-b18a-3929b85f9f68.png) + + # course material: 1. Introduction to the Python language 2. Variables From 9cef46c4a4608c8e9a61f24fe23ec90a8bd92222 Mon Sep 17 00:00:00 2001 From: Liron-Mizrahi <91504420+lironmiz@users.noreply.github.com> Date: Tue, 17 Jan 2023 19:40:46 +0200 Subject: [PATCH 6/8] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index eef80c5..66a458e 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ this repository contains solutions in python to the python course problems of th ![image](https://user-images.githubusercontent.com/91504420/212971858-3c5505ad-1228-4d45-b18a-3929b85f9f68.png) +![image](https://user-images.githubusercontent.com/91504420/212972245-cf436d2b-a5b1-4c04-8a2a-2a308f1bcc25.png) # course material: 1. Introduction to the Python language From 032f6bd1a28b320cea7ca1205cca38accbd654c6 Mon Sep 17 00:00:00 2001 From: Liron-Mizrahi <91504420+lironmiz@users.noreply.github.com> Date: Wed, 18 Jan 2023 12:34:27 +0200 Subject: [PATCH 7/8] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 66a458e..827580d 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,8 @@ this repository contains solutions in python to the python course problems of th ![image](https://user-images.githubusercontent.com/91504420/212971858-3c5505ad-1228-4d45-b18a-3929b85f9f68.png) +![image](https://user-images.githubusercontent.com/91504420/213149351-abc824f2-1300-414b-85b1-1955f995a4ed.png) + ![image](https://user-images.githubusercontent.com/91504420/212972245-cf436d2b-a5b1-4c04-8a2a-2a308f1bcc25.png) # course material: From 10c841a6e8a0ef433173f0507a9783972ae048bb Mon Sep 17 00:00:00 2001 From: Liron-Mizrahi <91504420+lironmiz@users.noreply.github.com> Date: Wed, 18 Jan 2023 14:02:00 +0200 Subject: [PATCH 8/8] Update README.md --- README.md | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 827580d..50669d3 100644 --- a/README.md +++ b/README.md @@ -19,25 +19,33 @@ this repository contains solutions in python to the python course problems of th

+# The Graduation Certificate 🀩 + +![SoExcited~GIF](https://user-images.githubusercontent.com/91504420/213165483-e7340f16-17b1-4c66-897a-0075e11677d9.gif) + ![image](https://user-images.githubusercontent.com/91504420/212971858-3c5505ad-1228-4d45-b18a-3929b85f9f68.png) +# Examiner's Notes 🫑 + ![image](https://user-images.githubusercontent.com/91504420/213149351-abc824f2-1300-414b-85b1-1955f995a4ed.png) +# Progress Graph πŸ“ˆ + ![image](https://user-images.githubusercontent.com/91504420/212972245-cf436d2b-a5b1-4c04-8a2a-2a308f1bcc25.png) -# course material: -1. Introduction to the Python language -2. Variables -3. String in python -4. Conditions -5. functions -6. Lists -7. Loops -8. Advanced data structure e.g (tuple, Dictionary ) -9. Files -10. Final project from scratch hangman game. - -# video of the final project πŸ€™ +# Course Material: + * Introduction to the Python language + * Variables + * String in python + * Conditions + * Functions + * Lists + * Loops + * Advanced data structure e.g (tuple, Dictionary ) + * Files + * Final project from scratch hangman game. + +# Video Of The Final Project πŸ€™ https://user-images.githubusercontent.com/91504420/210758896-10ba6b90-9aa2-49ca-8f73-7fdf5c3ed5b0.mp4