From 4f2dd9107a0a55eefd842c18caf64b60f1c15a6d Mon Sep 17 00:00:00 2001 From: Liron-Mizrahi <91504420+lironmiz@users.noreply.github.com> Date: Thu, 5 Jan 2023 07:32:00 +0200 Subject: [PATCH 1/8] Create unit9_ex9.3.2.py --- unit9_ex9.3.2.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 unit9_ex9.3.2.py diff --git a/unit9_ex9.3.2.py b/unit9_ex9.3.2.py new file mode 100644 index 0000000..4a21691 --- /dev/null +++ b/unit9_ex9.3.2.py @@ -0,0 +1,63 @@ +# exercise 9.3.2 from unit 9 +''' +def my_mp4_playlist(file_path, new_song): +The function accepts as parameters: + +Path to the file (string). +Just like in the previous exercise, the file represents a playlist of songs and has a fixed format built from the name of the song, the name of the performer (singer/band) and the length of the song, separated by a semicolon (;) without spaces. + +An example of an input file called songs.txt: +Tudo Bom; Static and Ben El Tavori; 5:13; +I Gotta Feeling; The Black Eyed Peas; 4:05; +Instrumental; Unknown; 4:15; +Paradise; Coldplay; 4:23; +Where is the love?; The Black Eyed Peas; 4:13; +A string representing the name of a new song. +The operation of the function my_mp4_playlist: + +The function writes to the file the string representing the name of a new song (new_song) instead of the name of the song that appears in the third line of the file (if the file contains less than three lines, write empty lines to the file so that the name of the song is written in the third line). +The function prints the contents of the file after the change has been made. +An example of running the my_mp4_playlist function on the songs.txt file +>>> my_mp4_playlist(r"c:\my_files\songs.txt", "Python Love Story") +Tudo Bom; Static and Ben El Tavori; 5:13; +I Gotta Feeling; The Black Eyed Peas; 4:05; +Python Love Story;Unknown;4:15; +Paradise; Coldplay; 4:23; +Where is the love?; The Black Eyed Peas; 4:13; +The contents of the songs.txt file after + +Tudo Bom;Static and Ben El Tavori;5:13; +I Gotta Feeling;The Black Eyed Peas;4:05; +Python Love Story;Unknown;4:15; +Paradise;Coldplay;4:23; +Where is the love?;The Black Eyed Peas;4:13; + +''' +def my_mp4_playlist(file_path, new_song): + with open(file_path, 'r') as f: + lines = f.readlines() + + while len(lines) < 3: + lines.append(";;\n") + + song_parts = new_song.split(';') + song_name = song_parts[0] + artist = "Unknown" + if len(song_parts)> 1: + artist = song_parts[1] + + lines[2] = f"{song_name};{artist};\n" + + with open(file_path, 'w') as f: + f.writelines(lines) + + with open(file_path, 'r') as f: + print(f.read()) + +def main(): + my_mp4_playlist(r"c:\my_files\songs.txt", "Python Love Story") + +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: 2023εΉ΄1ζ17ζ₯ 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
+ + + # 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: 2023εΉ΄1ζ17ζ₯ 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  + # 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: 2023εΉ΄1ζ18ζ₯ 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  + +  # course material: From 10c841a6e8a0ef433173f0507a9783972ae048bb Mon Sep 17 00:00:00 2001 From: Liron-Mizrahi <91504420+lironmiz@users.noreply.github.com> Date: 2023εΉ΄1ζ18ζ₯ 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 π€© + + +  +# Examiner's Notes π«‘ +  +# Progress Graph π +  -# 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