From 901de50f05be032fe38a11674fd8c9cf610d7dd1 Mon Sep 17 00:00:00 2001 From: Gerald Odhiambo Date: Tue, 9 Apr 2024 13:00:12 +0300 Subject: [PATCH 1/3] Refactored the code by allowing player to play more than once, improved on the variable names --- Madlibs/madlibs.py | 54 +++++++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 22 deletions(-) diff --git a/Madlibs/madlibs.py b/Madlibs/madlibs.py index 07b68a24..03ccdaf1 100644 --- a/Madlibs/madlibs.py +++ b/Madlibs/madlibs.py @@ -1,23 +1,33 @@ import random -print("Title : Eat, Drink, And Be Sick") -noun = [] -for i in range(4): - n = input("Enter noun : ") - noun.append(n) -plural = [] -for i in range(6): - pn = input("Enter plural noun : ") - plural.append(pn) -adjective = [] -for i in range(2): - a = input("Enter adjective : ") - adjective.append(a) -adverb = input("Enter adverb : ") -letter = input("Enter any letter : ") -body_part = input("Enter any body part : ") -print("An inspector from the Department of Health and ", random.choice(noun) , " Services paid a surprise visit to our " , random.choice(adjective) , " school cafeteria.") -print("The lunch special, prepared by our " , random.choice(adjective) , "dietician, was spaghetti and " , random.choice(noun) , " balls with a choice of either a " , random.choice(noun) , " salad or French " , random.choice(plural) , ".") -print("The inspector found the meat-" , random.choice(plural) , " to be overcooked and discovered a live " , random.choice(noun) , " in the fries,causing him to have a " + body_part + " ache.") -print("In response, he threw up all over his " , random.choice(plural) , ".") -print("In his report, the inspector " + adverb + " recommended that the school cafeteria serve only nutritious " , random.choice(plural) , " as well as low-calorie " , random.choice(plural) , " and that all of the saturated " , random.choice(plural) , " be eliminated.") -print("He rated the cafeteria a " + letter + "-minus.") + +attempts = 3 + +while attempts > 0: + print("Title : Eat, Drink, And Be Sick") + nouns = [] + for _ in range(4): + n = input("Enter noun: ") + nouns.append(n) + + plural_nouns = [] + for _ in range(6): + plural_noun = input("Enter plural noun: ") + plural_nouns.append(plural_noun) + + adjectives = [] + for _ in range(2): + a = input("Enter adjective: ") + adjectives.append(a) + + adverb = input("Enter adverb: ") + letter = input("Enter any letter: ") + body_part = input("Enter any body part: ") + + print("An inspector from the Department of Health and", random.choice(nouns), "Services paid a surprise visit to our", random.choice(adjectives), "school cafeteria.") + print("The lunch special, prepared by our", random.choice(adjectives), "dietician, was spaghetti and", random.choice(nouns), "balls with a choice of either a", random.choice(nouns), "salad or French", random.choice(plural_nouns), ".") + print("The inspector found the meat-", random.choice(plural_nouns), "to be overcooked and discovered a live", random.choice(nouns), "in the fries, causing him to have a " + body_part + "ache.") + print("In response, he threw up all over his", random.choice(plural_nouns), ".") + print("In his report, the inspector", adverb, "recommended that the school cafeteria serve only nutritious", random.choice(plural_nouns), "as well as low-calorie", random.choice(plural_nouns), "and that all of the saturated", random.choice(plural_nouns), "be eliminated.") + print("He rated the cafeteria a " + letter + "-minus.") + + attempts -= 1 From 3d535cc99707fd2981c8d028a8580636d2617b70 Mon Sep 17 00:00:00 2001 From: Gerald Odhiambo Date: Tue, 9 Apr 2024 14:11:26 +0300 Subject: [PATCH 2/3] Update madlibs.py Added several features such as: -Allowing the user to play more than once - validate user input -handle any errors --- Madlibs/madlibs.py | 65 +++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 27 deletions(-) diff --git a/Madlibs/madlibs.py b/Madlibs/madlibs.py index 03ccdaf1..82a6e0c9 100644 --- a/Madlibs/madlibs.py +++ b/Madlibs/madlibs.py @@ -1,33 +1,44 @@ import random +from sys import exit -attempts = 3 - -while attempts > 0: - print("Title : Eat, Drink, And Be Sick") - nouns = [] - for _ in range(4): - n = input("Enter noun: ") - nouns.append(n) - - plural_nouns = [] - for _ in range(6): - plural_noun = input("Enter plural noun: ") - plural_nouns.append(plural_noun) - - adjectives = [] - for _ in range(2): - a = input("Enter adjective: ") - adjectives.append(a) - - adverb = input("Enter adverb: ") - letter = input("Enter any letter: ") - body_part = input("Enter any body part: ") - - print("An inspector from the Department of Health and", random.choice(nouns), "Services paid a surprise visit to our", random.choice(adjectives), "school cafeteria.") +def get_user_input(prompt): + while True: + user_input = input(prompt) + if user_input: + return user_input.strip() + else: + print("Please enter a valid input.") + +def play_madlib(): + print("Title: Eat, Drink, And Be Sick") + nouns = [get_user_input("Enter noun: ") for _ in range(4)] + plural_nouns = [get_user_input("Enter plural noun: ") for _ in range(6)] + adjectives = [get_user_input("Enter adjective: ") for _ in range(2)] + adverb = get_user_input("Enter adverb: ") + letter = get_user_input("Enter any letter: ") + body_part = get_user_input("Enter any body part: ") + + print("\nAn inspector from the Department of Health and", random.choice(nouns), "Services paid a surprise visit to our", random.choice(adjectives), "school cafeteria.") print("The lunch special, prepared by our", random.choice(adjectives), "dietician, was spaghetti and", random.choice(nouns), "balls with a choice of either a", random.choice(nouns), "salad or French", random.choice(plural_nouns), ".") - print("The inspector found the meat-", random.choice(plural_nouns), "to be overcooked and discovered a live", random.choice(nouns), "in the fries, causing him to have a " + body_part + "ache.") + print("The inspector found the meat-", random.choice(plural_nouns), "to be overcooked and discovered a live", random.choice(nouns), "in the fries, causing him to have a " + body_part + " ache.") print("In response, he threw up all over his", random.choice(plural_nouns), ".") print("In his report, the inspector", adverb, "recommended that the school cafeteria serve only nutritious", random.choice(plural_nouns), "as well as low-calorie", random.choice(plural_nouns), "and that all of the saturated", random.choice(plural_nouns), "be eliminated.") print("He rated the cafeteria a " + letter + "-minus.") - - attempts -= 1 + + return get_user_input('\nDo you want to play again? (Yes/No): ').lower().startswith('y') + +def main(): + print("Starting the madlib game...\n") + play_again = True + while play_again: + play_again = play_madlib() + + print("You've chosen not to play.") + print('Exiting....') + exit(0) + +if __name__ == '__main__': + main() + + + From b2c50866280a5bd8dc90b96d137ae4935a8206eb Mon Sep 17 00:00:00 2001 From: Gerald Odhiambo Date: Tue, 9 Apr 2024 14:51:30 +0300 Subject: [PATCH 3/3] Add index.html for GitHub Pages --- index.html | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 index.html diff --git a/index.html b/index.html new file mode 100644 index 00000000..336c1be1 --- /dev/null +++ b/index.html @@ -0,0 +1,13 @@ + + + + + + My GitHub Pages Example + + +

Welcome to My GitHub Pages Example

+

This is a simple HTML file to demonstrate GitHub Pages.

+

You can add more content, styles, and scripts as needed for your project.

+ +