Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pthyon 3 question (newb)
#1
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()
Reply
#2
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
Reply
#3
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.
Reply
#4
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.
Reply
#5
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)
Reply
#6
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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)
Reply
#8
Yeah that is exactly what the same lesson did, in later steps to be completed.
Reply
#9
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
Reply
#10
You definitely can.
total = 0
total += 5
total += 1 + 2 + 3 + 4 + 5
print(total) # 20
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError issue with daughter's newb code MrGonk 2 1,406 Sep-16-2021, 01:29 PM
Last Post: BashBedlam
  Simple newb string question Involute 2 2,169 Sep-08-2019, 12:50 AM
Last Post: Involute
  please help this newb install pygame iofhua 7 5,845 May-15-2019, 01:09 PM
Last Post: buran
  Newb question: Debugging + Linting Python in Visual Studio Code Drone4four 1 2,388 Apr-15-2019, 06:19 AM
Last Post: perfringo
  Newb question about %02d %04d bennylava 30 19,146 Mar-05-2019, 11:23 PM
Last Post: snippsat
  newb selfie PatM 5 3,544 Feb-19-2019, 12:20 AM
Last Post: snippsat
  Complete NEWB and openpyxl project Netopia 44 16,878 Jan-18-2019, 08:15 PM
Last Post: Netopia
  Newb Question - Threading in Crons vvarrior 2 2,719 Jul-20-2018, 08:12 PM
Last Post: vvarrior
  Matt's newb question 1 MattSS102 1 2,669 Aug-28-2017, 03:27 AM
Last Post: BerlingSwe
  Newb: Simple Explicit Formula Duplicitous 1 3,108 May-05-2017, 07:03 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020