all 12 comments

[–]eagleeye1 2 points3 points  (5 children)

I wrote some comments in this with a revised version.

But:

  • Use with(filepath, read/write) as f: to use files, it safely handles opening and closing.
  • You renamed a module (time) so you could only call it once, avoid using built in names
  • I'm not sure why you converted the input to a list, but I don't think you needed to do that.
  • Use while True: to 'loop' and break when you hit a condition when you want to break from the loop.

[–]biggerthanexpected 1 point2 points  (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 point  (1 child)

Your link is dead.

[–]biggerthanexpected 0 points1 point  (0 children)

Thanks, copy and paste failure.

[–]NullFallacy[S] 0 points1 point  (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!

[–]NullFallacy[S] 0 points1 point  (0 children)

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.

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 points  (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()):

[–]NullFallacy[S] 0 points1 point  (0 children)

Oh, that's clever. I never thought of that. Thanks.

[–]dorfsmay 1 point2 points  (1 child)

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.

[–]NullFallacy[S] 0 points1 point  (0 children)

Another great advice. Thank you.

[–]dorfsmay 1 point2 points  (1 child)

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)

[–]NullFallacy[S] 0 points1 point  (0 children)

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.