2015-09-05 03:49:08

I understand the idea of writing functions mostly, and how to call them, I was actually more curious how most programmers actually write code. When writing code do you have the brain process down to think inside of the program out out, or do you work outside in, writing what the user sees and working your way back to the basic logic of the game. I have done quite bit of HTML, and some java script and php. This is the farthest I have made it in a language though with python, and none of what I did required nearly as much logic as I have learned in python. So as a beginner to writing logic, at least on a greater scale than I ever have before, it still seems more logical to me to think from the user's end backwards.

2015-09-05 06:50:04

Hello,

I tend to write my main function at the bottom of the module just because it allows me to easily jump from the bottom line where I execute the main function to the main function. There is no need to declare a function in any order as long as you are running the function at the bottom of the module. For example:

def function1():
  print("Hello")

def function22():
  function1()
  function3()

def function3():
  print(" world")

function2()



I write using python 2 syntax. There are some tendencies that one can do that make python 2 and 3 compatible and this is what I try to do when ever I can. For example:

print("Hello world")

could also be written as:

print "Hello world"

but why do it like that? It is unclear and not compatible. print("Hello world") works on both 2.7 and 3.* and it tells us that print is a function... There are also some more advantages to treating print as a function. There are keyword arguments that one can use in print the function and not print the statement as well.
The only real difference is the input vs raw_input function. Other than that python 2 and python 3 are the same to newbies.
If you follow python3 for imports:

from .mymodule import myfunction

it works in both 2 and 3.

2015-09-05 07:01:15

Thanks, that clears up a lot for me. I had another question though, when naming variables or functions I see that most people seem to use underlines, I heard that you can also use another method where you capitalize the first letter of each word. Is there any particular standard or reason why most people use underlines? I kind of prefer using capitals because it is less confusing when Jaws reads a line of code.

2015-09-05 08:36:51

@TJ.Breitenfeldt

It's important to take some previous notes about how your program will work once finished before starting. If you don't, you will rewrite and rewrite code again. Example:
1. There will be a main menu.
2. THis menu will contain elements.
3. You can select each element and then something happens.

It could feel ridiculous, but if you didn't,... Example:
Oh, I have 4 different kinds of menus, each has its own features, but now I'd like to log all the activity, so... let's code our menus again.
Wait, I have implemented the logging feature, but now each menu element returns a string that is added to the log, and I'd like to make my program easy translatable, so I need that each element returns an integer that then is converted to a string according to the choosen language... Let's rewrite all again! Etc.

There are a lot of ways to organize your code, depending on your needs. A basic understanding of some of the most common design paterns would help you too. Take a look at the infamous Gang of Four book (GoF), or google for gang of four paterns:
http://www.gofpatterns.com/design-patte … nefits.php

Basically, a patern is an organization model for your code that works with any programing language. For example, the observer paterns (also known as event driven patern) implements a messaging sistem whitch allows you objects, functions or whatever, sending messages to other members, such like: hey there! I'm X and someone has just killed me. Or: hey, server, an user instance has send a message to chatroom X... But it's being a boring and long post, time to leave.

Hope this helps.

2015-09-05 10:39:32

Pep 8 is the bible of python style:
https://www.python.org/dev/peps/pep-0008/

Look at the heading:
Method Names and Instance Variables

Learn Python the Hard Way will have you go through this a couple times, so read it every few months. It is a really good idea to follow it in any code you right.
C++ and javascript have nothing like this and their code looks really really really messy to someone coming from python. I love python because of pep 8. That way you can tell what is what just by reading its name.

2015-09-08 05:16:39

Thank you so much for all of the help.