This forum is in permanent archive mode. Our new active community can be found here.

Weekend coding

edited November 2010 in Technology
Today I'm starting to write small programs in just a day, each weekend, and I'm currently dicking around with functions I've coppied out of a Python guide (The Computer Scientist one.), taking them and trying to get them to do something else. Something about the liniar reading through a book method is hard to stay focused on.

Total beginner question: I want to have a function take an input and then, once it's done it's thing, assign the output to a value. I'm also putting the function in a seperate file and importing it.

Focussing on the count_digits function.

NewTest.py
from UsefulFunctions import verttext,count_digits

string = "Strings are Stringy!"

verttext (string)

count_digits (5000)

print count


Usefulfunctions.py
def verttext (input):
index=0
while index < len(input):
print input[index]
index += 1

def count_digits (input):
count = 0
while input:
count = count + 1
input = input / 10
return count
«13456739

Comments

  • All you need to do is:
    num = count_digits(5000)

    The value of the expression "count_digits(5000)" is precisely the value returned by the function.

    Also, verttext could just be:
    for c in input: print c
  • "count = count_digits(5000)" Works!

    I'll look at compacting code later.
  • I'm also learning a bit of coding. Can you link the guide you're using?
  • edited November 2010
    How to Think Like a Computer Scientist.

    Another question: Is it better to use "print functionx (input)" than putting the print part inside the function (Relating to the verttext function.)? As I can see it becoming more versatyle that way.

    Problem is, I then need to have it return the whole lot, so I'd need to add it all into a string or similar.
    Post edited by Omnutia on
  • edited November 2010
    Another question: Is it better to use "print functionx (input)" than putting the print part inside the function (Relating to the verttext function.)? As I can see it becoming more versatile that way.
    Use the first if you only want to print the final output (return) of a function, and don't need that output later. Use the later(inside the function) for debugging or if you want to see a value that only the function needs to see.
    As for what it looks like you're trying to do (print each character) how you have it is probably best simply because it reduces the number of function calls, and putting in a return so you can print it here would defeat the purpose of having the function.
    Also look into for loops, unless told specifically to use while in your guide. They do what your trying to do in while loops easier.
    Post edited by Shiam on
  • It doesn't seem like you know how to do string formatting or use tuples. Here I show both together.

    x = 3
    y = 5
    print "The value of x is %s and the value of y is %s" % (x, y)
    print "The value of x is %s" % x
    x, y = (6, 2)
    z = (x, y)
    print "The value of x is %s and the value of y is %s" % z
  • Examples are generally precieded and/or followed by a description, but I'll have a shot at working that out.
  • Examples are generally precieded and/or followed by a description, but I'll have a shot at working that out.
    It's pretty self explanatory. If you run that example, and it isn't self explanatory, then you've got to go back to even more basics.
  • A description of how it works would be helpful for working out what else I can apply it to.
  • %s is a control/formatting character that tells print to look for a value to put in it. You put the values that go in the various %s in a list after string. The %s also tells the system what type the variable you are putting in is
    The (value, value)/value, value syntax are tuples, essentially a list of items but individual items cannot be directly changed after assignment.
    For some of this familiarize yourself with the python docs, they're quite good.
  • That %s trick is going to come in super handy with the shit scripts I write. I know my scripts are super inefficient, but I seem to solve every problem I find without looking up any new techniques or functions. Check out my latest:

    import string

    start = 30
    end = 111

    csv = "episode number\tauthor\tbook\tlength\tsize\tblog post number\trating\tblog post\tnext book\tnotes and corrections\twiki link\tamazon.com link\t\n"

    def removecharfrontandendofstring(r, char):
    if len(r) <= 1:
    return r
    d = r
    if d == "":
    return r
    while True:
    if len(r) <= 1:
    return r

    if d[0] == char:
    e = d[1:]
    d = e
    continue
    break
    while True:
    if len(r) <= 1:
    return r
    if d[-1] == char:
    e = d[:-1]
    d = e
    continue
    break
    r = d
    return r

    def getdatafromtxtin(filename):
    data = ""
    f1 = open(filename, "r")
    while True:
    text = f1.readline()
    if text == "":
    break
    if text[0] == "#":
    continue
    data = data + text
    f1.close()
    return data

    h = start
    while h < end:
    newline = ""
    if h < 100:
    episode = str(h)
    episode = "0" + episode
    else:
    episode = str(h)
    txtin = "/Users/lukeburrage/Podcasts/SFBRP/Notes/" + episode + ".txt"
    data = getdatafromtxtin(txtin)
    dl = string.split(data, "\n")
    i = 0
    while i < len(dl):
    dl[i] = removecharfrontandendofstring(dl[i], " ")
    newline = newline + dl[i] + "\t"

    i = i + 1
    newline = newline + "\n"
    csv = csv + newline
    h = h + 1

    print csv</pre>

    Of course, that will make no sense to anyone, but it shows just how much of the really basic stuff that I don't know, and don't even know that I don't know.
  • removecharfrontandendofstring? Did you not even try to read the documentation?

    http://docs.python.org/library/string.html
  • Did you not even try to read the documentation?
    No.
  • edited November 2010
    If "string.strip" is depreciated, what should I use instead?


    EDIT. got it.

    That function is one I wrote as an exercise years ago, and just copied it into this code. Of course, reading documentation would make my code way cleaner, but the point is I don't do this frequently enough to remember.
    Post edited by Luke Burrage on
  • edited November 2010
    I finished reading the book last week, along with some of Python's documentation.
    Does anyone know an ongoing python project which could use my help?
    I don't have previous experience with coding, so I probably can't contribute a lot, but at least I could learn from others.

    The alternative is to code a roguelike, but I don't want to start a big project without someone to look over my shoulder, and tell me where can I can improve the code.
    Post edited by Adelbert on
  • edited November 2010
    That function is one I wrote as an exercise years ago, and just copied it into this code. Of course, reading documentation would make my code way cleaner, but the point is I don't do this frequently enough to remember.
    This is my job that pays my bills, and I don't remember. I look it up every single time. I have the documentation to whatever I'm working on open at all times, and I frequently look up the same things over and over again. Before I code anything, I Google search to make sure it hasn't already been done and there isn't already an easier way to do it.
    Post edited by Apreche on
  • The alternative is to code a roguelike, but I don't want to start a big project without someone to look over my shoulder, and tell me where can I can improve the code.
    Just make it. If something is slow or broken, learn why and discover how you can fix it yourself. Having a mentor doesn't really do anything.
  • edited November 2010
    Thanks for posting the docs Scott. I got bored, looked at the first part of it and used my knowledge of for loops, the in keyword and if statements to create a program that returns the punctuation in a string. It's pretty useless, but it was a good way to test my knowledge.
    Post edited by Pegu on
  • This is my job that pays my bills, and I don't remember. I look it up every single time. I have the documentation to whatever I'm working on open at all times, and I frequently look up the same things over and over again.
    This.
  • This is my job that pays my bills, and I don't remember. I look it up every single time. I have the documentation to whatever I'm working on open at all times, and I frequently look up the same things over and over again.
    This.
    Even in my programming exam, I was allowed to look at the documentation. Being a good programmer does not mean memorising all the functions in a language.
  • edited November 2010
    Thankfully this isn't my job, or else I wouldn't get paid! It's good to know that all the professionals look at documentation all the time.

    I'm not trying to defend bad working practices here, but the way I look at it is that I do this so rarely, that I don't even know WHAT to look up. For example:

    if h < 100:
    episode = str(h)
    episode = "0" + episode
    else:
    episode = str(h)
    I want to take a value, and if it has only two digits, add a leading zero. And then make the value into a string, with three characters. With something like this, I have no idea where to start searching for the pre-existing function. Can you make an int have a leading zero? I'm not sure. Adding a leading zero to a string is super easy, and now that I think about it, I guess I could add it to any string shorter than three characters very simply.

    However, to write that bit of code took way less time than looking up pre-existing functions, especially because I didn't have internet access at the time, and don't have the documentation to hand. I know if this way my job, every time I looked something up I'd be saving more and more and more time in the long run, but I write scripts like this once every few months, at the very most, and I can't bring myself to put in that extra work to save so little over the long term. One day I will, and would love to get some tuition on python scripting, just so I can widen my knowledge of what is possible, but for now I'll concentrate on making shit.

    Actually, the next shit I make is a way to display, in HTML, all the information I collected into a spreadsheet with the above code. I'll look up as many functions as possible, and hope to spit out some beautiful code.
    Post edited by Luke Burrage on
  • Even in my programming exam, I was allowed to look at the documentation. Being a good programmer does not mean memorizing all the functions in a language.
    This surprises me a bit mostly because all of my CS exams have had sections where you need to write code without reference. Though generally you weren't graded for syntax errors so much as getting the right idea. I agree the with the idea though, I also constantly look things up.
    @luke A Google search out of curiosity says you can do that code in one line, but it was something I vaguely remembered you can do with %s. ;P
  • Even in my programming exam, I was allowed to look at the documentation. Being a good programmer does not mean memorizing all the functions in a language.
    This surprises me a bit mostly because all of my CS exams have had sections where you need to write code without reference. Though generally you weren't graded for syntax errors so much as getting the right idea. I agree the with the idea though, I also constantly look things up.
    @luke A Google search out of curiosity says you can do that code in one line, but it was something I vaguely remembered you can do with %s. ;P
    I had sections where you're supposed to look at code and figure out what's going on, or answer questions on concepts. There wasn't any documentation allowed then. The bits where I had to write actual code allowed documentation.
  • That is the one advantage of a programming education. When I search Google I know the magic words and jargon to type in. You would search for "add a leading zero" I would search for "python string formatting".

    If you want to do HTML you should use a template system. I suggest jinja2. I used it in my presentoh app.
  • edited November 2010
    Yep, this is just another use of string formatting. Again, most people wouldn't know offhand what magical syntax you need to get the right result, but if you look it up you find %03d is what you want.
    Post edited by lackofcheese on
  • edited November 2010
    I'm not sure where you find %03d, but I've think str.ljust(3,'0') might work.
    Post edited by Luke Burrage on
  • edited November 2010
    ljust would work, although what you would need is str.ljust(3, '0').
    EDIT: Heh, you pre-empted me there.
    The square brackets in the argument list mean that the argument is optional, and it wants a character argument for fill with, whereas 0 is an integer.

    I didn't want to give away too much, but %03d is formatting syntax, the same as %s before. String formatting is described here.

    It's important to note that in the most recent versions of Python there is in fact a new formatting syntax which is somewhat more powerful than the old %-style; see here.
    Post edited by lackofcheese on
  • What's the best way to check several conditions at once and go to else if any one of them isn't met?
  • Err, don't you just mean
    if X and Y and Z: f()
    else: g()
  • What's the best way to check several conditions at once and go to else if any one of them isn't met?
    /sigh. I will hold back my typical rant about how this is the wrong question to ask. Just tell us exactly what you are trying to do. Show the code and such.
Sign In or Register to comment.