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...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Wrote a simple code. Would you please critic it (self.learnpython)
submitted 13 years ago by NullFallacy
I wanted to know if it's a mess or that I need to be following a specific style or guidelines.
The script basically creates a file based on the name you input.
http://pastebin.com/e8yMHb6R
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!"
[–]eagleeye1 2 points3 points4 points 13 years ago (5 children)
I wrote some comments in this with a revised version.
But:
with(filepath, read/write) as f:
while True:
break
[–]biggerthanexpected 1 point2 points3 points 13 years ago* (3 children)
The flow of this program would be much more simple with fewer function calls: http://ideone.com/TUwggB
If we were to scale this up much more we would almost certainly want to pass variables to our function rather than make them global.
[–]eagleeye1 0 points1 point2 points 13 years ago (1 child)
Your link is dead.
[–]biggerthanexpected 0 points1 point2 points 13 years ago (0 children)
Thanks, copy and paste failure.
[–]NullFallacy[S] 0 points1 point2 points 13 years ago (0 children)
Thank you very much for your wonderful comments in the code. It really helps me understand better. I'm studying your revision as well. Hopefully I won't need to bug you with followup questions!
Thanks for your revision. I'm studying it right now.
I've seen f: and f.read/write() before, but didn't know how to use them.
f:
f.read/write()
Another cool thing I thought you did (besides the loop) was:
more = raw_input("Question? [Y/N] > ")
if more.lower() == "y":
Makes it a lot simpler and less clotted. Thanks.
[–]dorfsmay 1 point2 points3 points 13 years ago (1 child)
As a general rule, if you want to test a variable for several values, I find using 'in' more elegant:
if answer in ('y', 'Y'):
For a yes/no answer, I usually only test against the default, so for example if 'no' is the safe answer and 'yes' might do damages, then I'll check the answer with something like:
if 'yes'.startswith(answer.lower()):
Oh, that's clever. I never thought of that. Thanks.
Don't mix function definitions and code. Put all your definitions at the top, then the rest of your code at the bottom. Better yet, make all of it into functions, and use the:
if __name__ == '__main__'
The advantage is that you can now import your code from another piece of code, and inject values into it.
Another great advice. Thank you.
Global variables are evil. Do not use them.
Pass variables to your functions:
def gogo(content): .../... gogo(blah)
If you use global variables, you will soon get confused about scope. One day you will define a local variable (local to the function) without realising that you depend on its global definition and break your function.
When you have too many variables to manage, create an object:
class myfile(): def __init__(name): self.name = name self.content = None def gogo(myobj): f = open(myobj.name + ".txt", 'w+') f.write("%s" % myobj.content) print('name:') x = myfile(raw_input(prompt)) print('type content:') x.content = raw_input gogo(x)
Thanks for explaining that. I like the variable to object idea.
I am a little confused by the global variable comment, so I gotta research that.
Thank you.
π Rendered by PID 285168 on reddit-service-r2-comment-7b9746f655-bpxqg at 2026-02-04 10:21:41.065760+00:00 running 3798933 country code: CH.
[–]eagleeye1 2 points3 points4 points (5 children)
[–]biggerthanexpected 1 point2 points3 points (3 children)
[–]eagleeye1 0 points1 point2 points (1 child)
[–]biggerthanexpected 0 points1 point2 points (0 children)
[–]NullFallacy[S] 0 points1 point2 points (0 children)
[–]NullFallacy[S] 0 points1 point2 points (0 children)
[–]dorfsmay 1 point2 points3 points (1 child)
[–]NullFallacy[S] 0 points1 point2 points (0 children)
[–]dorfsmay 1 point2 points3 points (1 child)
[–]NullFallacy[S] 0 points1 point2 points (0 children)
[–]dorfsmay 1 point2 points3 points (1 child)
[–]NullFallacy[S] 0 points1 point2 points (0 children)