It looks like you're new here. If you want to get involved, click one of these buttons!
from UsefulFunctions import verttext,count_digits
string = "Strings are Stringy!"
verttext (string)
count_digits (5000)
print count
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
Comments
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
I'll look at compacting code later.
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.
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.
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.
http://docs.python.org/library/string.html
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.
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.
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: 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.
@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
If you want to do HTML you should use a template system. I suggest jinja2. I used it in my presentoh app.
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.
if X and Y and Z: f()
else: g()