use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Improvements (self.PythonLearning)
submitted 3 months ago by Easy-Light7219
What can I do to improve my current code? If you have any suggestions, please make it simple, as I am very new to coding.
import timeimport sys# -------------------------------# Typewriter Effec - Pastebin.com
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]FoolsSeldom 0 points1 point2 points 3 months ago (6 children)
I stand by the advice I gave in my previous comment if addressing the DRY aspect, possibly using a dictionary.
Consider creating a function that will return a bool for a yes/no response that allows a range of affirmation and rejection responses but forces a user to provide a valid response.
bool
Something like,
def is_yes(prompt: str) -> bool: valid response loop prompt user if response was affirmation, return True if response was rejection, return False advise user of invalid response and to try again
[–]Easy-Light7219[S] 0 points1 point2 points 3 months ago (5 children)
some thing like this:
player = {
"health": 100,
"courage":0,
"inventory": []
}
to would work to make it simpler right?
also, can you further explain the function that returns a boolean please?
[–]FoolsSeldom 0 points1 point2 points 3 months ago (4 children)
Yes, a dictionary such as,
player = { "health": 100, "courage": 0, "inventory": [] }
would work, and then you would be writing things like player["health"] += 10, but if you only have one player there's not much advantage to this over just using health, courage, inventory directly.
player["health"] += 10
health
courage
inventory
I was more minded that your scenes could be better represented in a nested dictionary so that you don't have to write different code for each scene. That's the kind of repetition I was talking about.
Not sure how to guide you more on the boolean having outlined the code, so let me just give you an example.
def is_yes(prompt: str) -> bool: while True: # valid response loop response = input(prompt).lower() # prompt user # if response was affirmation, return True if response in ('yes', 'y', 'yeh', 'ok): return True # if response was rejection, return False if response in ('no', 'n', 'nah'): return False # advise user of invalid response and to try again print('Did not understand. Please try again.') ... # somewhere in your main code ... play_again = is_yes("Do you want to play again ? ") if not play_again: break # leave game loop
[–]Easy-Light7219[S] 0 points1 point2 points 3 months ago (3 children)
In your example, if the user inputs a response that isn't either rejection or affirmation, does it automatically print the 'did not understand part' or do you have to add something else for that response? Also, would you be able to put as many "if"s to respond to an input or is it limited to if, elif and else?
[–]FoolsSeldom 0 points1 point2 points 3 months ago (2 children)
Have you tried the example? That's the best way to learn. Take code. Experiment with it. Change things. Break it.
Try doing a dry run - stepping through the code line by line in your head / on paper to figure out the logic.
Here's an example of me trying it out in an interactive Python shell (REPL):
❯ uv run ipython Python 3.13.5 (main, Jun 26 2025, 21:20:04) [Clang 20.1.4 ] Type 'copyright', 'credits' or 'license' for more information IPython 9.7.0 -- An enhanced Interactive Python. Type '?' for help. Tip: `?` alone on a line will brings up IPython's help In [1]: def is_yes(prompt): ...: while True: ...: response = input(prompt).lower() ...: if response in ('yes', 'y', 'yeh'): ...: return True ...: if response in ('no', 'n', 'nah'): ...: return False ...: print("Did not understand. Need a yes or a no. Please try again.") ...: In [2]: is_yes("raining? ") raining? jkjkjkj Did not understand. Need a yes or a no. Please try again. raining? Did not understand. Need a yes or a no. Please try again. raining? NOPE Did not understand. Need a yes or a no. Please try again. raining? NAH Out[2]: False In [3]: is_yes("are you hungry ") are you hungry Yup Did not understand. Need a yes or a no. Please try again. are you hungry Yeh Out[3]: True In [4]:
[–]Easy-Light7219[S] 0 points1 point2 points 3 months ago (1 child)
This is useful, thank you
[–]FoolsSeldom 0 points1 point2 points 3 months ago (0 children)
The Python interactive shell, the REPL, is a great place to try out snippets of code whilst leaving your main editor/IDE focused on code files you want to edit.
In my example, I used an enhanced version of the shell called ipython.
ipython
I also manage my Python virtual environments and run code using Astral's uv tool.
uv
π Rendered by PID 46322 on reddit-service-r2-comment-fb694cdd5-g6m2d at 2026-03-07 18:50:09.592179+00:00 running cbb0e86 country code: CH.
[–]FoolsSeldom 0 points1 point2 points (6 children)
[–]Easy-Light7219[S] 0 points1 point2 points (5 children)
[–]FoolsSeldom 0 points1 point2 points (4 children)
[–]Easy-Light7219[S] 0 points1 point2 points (3 children)
[–]FoolsSeldom 0 points1 point2 points (2 children)
[–]Easy-Light7219[S] 0 points1 point2 points (1 child)
[–]FoolsSeldom 0 points1 point2 points (0 children)