Progress Report Two: Learn Python 3 The Hard Way

in #python2 years ago

I just finished exercise 36, Designing and Debugging, from Learn Python 3 The Hard Way. I would have liked to work with some for loops, but to be honest, it took me so freaking long to figure out how to format the text decently that I decided I need to move on. There is not much to the program obviously, and yet it took me several days to get it into its current state. I am sure there are bugs I did not find, and I know there are standards and practices that are not being used. However, this is all new to me, and I have not learned those standards and practices so...

Initially I was using the code in the text_formatter(text) function on each block of text before I realized this was stupid and should be put into a function of its own. The reason it was tricky was that I could either keep my text looking good in the terminal or in the code, but not both. Eventually, this was the solution I found.

It is still unclear to me why I have to use from sys import exit and only import for textwrap and random. Again, I spent a crazy amount of time researching options for formatting the text, and decided it was time to move on.

In addition, it seems to me that were someone going to design an actual text based RPG game (and why in the world would they not be, after all it is all the craze these days) it would be beneficial to have the "story" in another file and pull it in somehow, but maybe that is not how it is done.

from sys import exit
import textwrap, random

def start():
    splash()
    print("The world is a dangerous place. What is your plan?")

    while True:
        choice = input("I will 'buy a gun', learn 'karate', call '911': ")

        if choice == "buy a gun":
            buy_gun()
        elif choice == "karate":
            karate_text = text_formatter("""
            You are eating breakfast when Ass Hat walks in and starts pushing
            around the waitress. You ask him to stop and he tries to punch
            you. Little does Mr. Hat know that you learned the ancient art of
            Karate! Press ENTER to see what happens...""")
            print("\n" + karate_text)
            input('')
            your_path("karate")
        elif choice == "911":
            nine_one_one_text = text_formatter("""
            An intruder is trying to get into your home. You call 911. While
            you are talking to them, the intruder breaks a window, and enters
            your home. You hide under the bed and wait. Press ENTER to see what
            happens.""")
            print("\n" + nine_one_one_text)
            input('')
            your_path("911")
        else:
            print("\nThat is not an option...")

def buy_gun():
    buy_gun_response = text_formatter("""
    Great choice! After some research, you visit your local gun shop. Do you
    buy the Kimber Master Ultra Cary 1911 45ACP, or the polymer framed
    Springfield Armory XD Mod.2 sub-compact 9mm?""")
    print("\n" + buy_gun_response)

    while True:
        my_gun = input("Choose your weapon: 'Kimber', 'Springfield Armory': ")

        if my_gun == "Kimber":
            kimber_text = ("""
            You have great taste! Kimber makes a fantastic 1911 even if you did
            pay $1,500 for it! You are in the park making fun of people wearing
            flat brim hats and skinny jeans. A man walking a dog tells you he
            needs food for his dog and you are it. He pulls out a knife. Press
            ENTER to see what happens.""")
            print("\n" + kimber_text)
            input('')
            your_path("Kimber")
        elif my_gun == "Springfield Armory":
            springfield_armory_text = text_formatter("""
            Fantastic choice! You would have spent $1,000 more for the Kimber!
            """)
            print("\n" + springfield_armory_text)
            reloader()
        else:
            print("\nThat is not an option...")

def reloader():
    ryoa = input("Use the extra money to roll your own ammo 'yes' or 'no'? ")

    if ryoa == "yes":
        ryoa_yes_text = text_formatter("""
        You are now a boss gun owner! You reload you own ammo and
        spend lots of time at the range. One day while you are
        out doing horse photography Butt Reamer approaches you, pulls out
        a gun and tell you to take off your pants. You reach for your belt
        with one hand and draw your pistol with the other. Double tap
        center mass. Mr. Reamer is dead and your bunghole is
        safe.""")
        print(ryoa_yes_text)
        winner()
    elif ryoa == "no":
        your_path("Springfield Armory")
    else:
        print("\nThat is not an option...")
        reloader()

def your_path(path):
    if path == "911" and random.randint(1, 10000) == 1:
        winner()
    elif path == "karate" and random.randint(1, 100) == 1:
        winner()
    elif path == "Kimber" and random.randint(1, 10) == 1:
        winner()
    elif path == "Springfield Armory" and random.randint(1, 5) == 1:
        winner()
    else:
        loser(path)

def loser(path):
    if path == "911":
        print("You are dead. Better luck next time. Oh, wait.")
        keep_going()
    elif path == "karate":
        print("You are dead. Maybe your belt is not the right color?")
        keep_going()
    elif path == "Kimber":
        print("You are dead. Disengage the safety next time! Stupid 1911's.")
        keep_going()
    elif path == "Springfield Armory":
        print("\nYou are dead. Too bad you could not afford to practice more.")
        keep_going()
    else:
        exit(0)

def keep_going():
    play_again = input("Try again? 'yes' or 'no': ")
    if play_again == "yes":
        start()
    elif play_again == "no":
        exit(0)
    else:
        keep_going()

def text_formatter(text):
    formatted_text = textwrap.dedent(text).strip()
    formatted_text = textwrap.fill(formatted_text, width = 75)
    return(formatted_text)

def splash():
    print("""
                 ___      _______  _______  __   __  _     _
                |   |    |       ||       ||  | |  || | _ | |
                |   |    |    _  ||_     _||  |_|  || || || |
                |   |    |   |_| |  |   |  |       ||       |
                |   |___ |    ___|  |   |  |       ||       |
                |       ||   |      |   |  |   _   ||   _   |
                |_______||___|      |___|  |__| |__||__| |__|

                     https://learnpythonthehardway.org
              ASCII conversion: http://patorjk.com/software/taag

       Instructions: choose your answer from inside the single quotes.
=============================================================================
""")

def winner():
    print("""
=============================================================================
             __   __  _______  __   __    _     _  ___   __    _
            |  | |  ||       ||  | |  |  | | _ | ||   | |  |  | |
            |  |_|  ||   _   ||  | |  |  | || || ||   | |   |_| |
            |       ||  | |  ||  |_|  |  |       ||   | |       |
            |_     _||  |_|  ||       |  |       ||   | |  _    |
              |   |  |       ||       |  |   _   ||   | | | |   |
              |___|  |_______||_______|  |__| |__||___| |_|  |__|

=============================================================================
""")
    keep_going()

start()
Sort:  

I was showing my kids my awesome game, and found a bug. I forgot to send kimber_text to the text_formatter(text) function. Be sure you fix this bug before you spend hours playing the game!

Nice photos. they deserve an award. It is very nice. great work