Python Forum

Full Version: Pthyon 3 question (newb)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
This is something from my python 3 lessons at CodeAcademy. As you can see there are two print functions. But if you remove the indentation from the second print function, it swaps the order in which they appear on the console. So if you remove the indent, then suddenly "This is your desktop" appears on top, instead of the other one. I was thinking it always read top to bottom. Why does python not print these two print functions in order from top to bottom?

def about_this_computer():
  print("This computer is runnong on Whackintosh versio Everest Puma")
print("This is your desktop")
  
about_this_computer()
Python does read from top to bottom but about_this_computer() is a function, code inside a function will only get executed when the function is called
But still, it would've had to have read the first print function before that, thus putting it in line to be printed first. It didn't know that we were going to call that function down there, until it had already been told to print the first print function.
Functions don't do anything until you call them. So if you comment out the last line, the only line you'll see is "This is your desktop". Python doesn't run the code as-it-sees-it, because you might never call a function, or you might call it multiple times. So the only code that's run is the code you tell it to run.
Thanks both for the replies. Here is another part of the lesson that I don't really get. You can see that I had to put a parameter called 'number' into the function definition. But then later on, I called the function with '1' in the function instead of 'number'. How come this doesn't confuse python? After all, I said that 'number' was in there, and not '1'. Is it because I'm no longer defining the function? And now instead, I'm calling it? And thus, it has already integrated 'number' into the function? So now I can put whatever else I want in there when I call it?

def mult_two_add_three(number):
  print(number*2 + 3)
  
mult_two_add_three(1)
You can think for number as a placeholder or alias of what you will supply when you call the function. At the time you define the function you don't know what will come, so you just use a name. Later on when you call the function, the python knows to replace number with whatever you supply
I like to think of a program like an assembly line. A function is one spot along that line. At that spot in the assembly line, the function will always do the same operation to whatever's on the line, then put something else on the line when it's done (which might then get passed to another function further down the assembly line). At this particular spot, the function will not know what will be put on the assembly line, it just knows that it'll be some kind of a number.

You could, for example, load that assembly line up with lots of tasks for the function to work through, which would look like this:
def mult_two_add_three(number):
  print(number*2 + 3)
   
mult_two_add_three(1)
mult_two_add_three(-12)
mult_two_add_three(3.14)
mult_two_add_three(1)
mult_two_add_three(1)
mult_two_add_three(42)
Yeah that is exactly what the same lesson did, in later steps to be completed.
Well its time for another newb question I guess: Why can't you just string += together as many times as you want? Or perhaps just say + as many times as you want? Instead of having to update the variable manually. You can see an example below, of what I'm talking about. As you can see, I had to type out the variable each time I wanted to add another +=. Seems like I should have just been able to daisy chain += += += +=... as many times as I wanted. Just curious on this one I guess. (its from codeacademy, part of a receipt printing project)

customer_one_total = 0
customer_one_itemization = ""
customer_one_total += 254.00
customer_one_itemization += lovely_loveseat_description
customer_one_total += 52.15
customer_one_itemization += luxurious_lamp_description
customer_one_tax = customer_one_total * sales_tax
customer_one_total = customer_one_total + customer_one_tax
You definitely can.
total = 0
total += 5
total += 1 + 2 + 3 + 4 + 5
print(total) # 20
Pages: 1 2