Hi. For the past week, I've been following an online Python guide named: 'Learn Python the Hard Way'. I'm very happy with it as it gives a lot of freedom to explore.
However, due to this I have no idea if I'm thinking the right way. That's why I've attached a script of mine I've been working on all day. It works a 100%, but I'm afraid I've made very bad choices concerning design and coding style. (If it could've been much simpler, if there are glaring mistakes, poor methods, ..) Could anyone be so friendly as to offer a bit of feedback, to a newbie? PS: The script very simple accepts 2 arguments from the commandline. First arg being the number to which should be counted, second arg being the interval. Thank you, Adrian
#!/usr/bin/env python # Made with python2.7 from sys import argv from sys import exit script, maxnumber, increment = argv maxnumber = int(maxnumber) increment = int(increment) jibjab = 0 numbers = [] askchange = 1 # The script goes as following: # When you start it and give two arguments, check_OP_size # checks wether you won't get too much output. # If you PASS the test, you're immediately sent to the main # function (the_loop, which is performing the actualy goal # of the script). If you FAIL you're sent to ask_change. # ask_change() notifies you that you're going to get # a lot of output and optionally lets you change your arguments. # You're then sent to choice_result which check if your response # is a valid response (e.g. Y/N). If it's not, you're sent to # jibjab_result that gives you 3 chances to type in Y/N. # If you fail typing Y/N three times, the app quits. # If you make it however, you're sent to: change_vars. # Of course, change_vars also immediately sends you to check_OP_size # to check for OP size. ##### ANY COMMENTS WELCOME AT howit...@archlinux.us def check_OP_size(maxn, incr): global choice, maxnumber, increment if maxn / incr > 10: ask_change() if maxn / incr <= 10: the_loop(maxnumber, increment) def ask_change(): global choice, maxnumber, increment print "You will recieve a lot of output.\nDo you wish to change your data?" choice = raw_input("Y/N: ") choice_result() def jibjab_result(): global choice, jibjab if jibjab < 2: jibjab += 1 choice = raw_input("Y/N: ") choice_result() elif jibjab >= 2: print "Idiot." exit() def choice_result(): global choice, maxnumber, increment if choice == "Y": change_vars() elif choice == "N": the_loop(maxnumber, increment) else: jibjab_result() def change_vars(): global maxnumber, increment maxnumber = raw_input("Give a new max number: ") increment = raw_input("Give a new increment: ") maxnumber = int(maxnumber) increment = int(increment) check_OP_size(maxnumber, increment) # the_loop(maxnumber, increment) def the_loop(maxn, incr): """Advances a number with the supplied increment untill it has reached the passed maximum number.""" maxn = int(maxn) incr = int(incr) i = 0 # for i in range( 0, maxn, incr): while i < maxn: print "At the top i is %d" % i numbers.append(i) i = i + incr print "Numbers now: ", numbers print "At the bottom i is %d" % i check_OP_size(maxnumber, increment) print "The numbers: " for num in numbers: print num,
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor