Python Forum
Trouble with LPtHW exercise 21
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trouble with LPtHW exercise 21
#1
Hi all

Im very new to programming and started Learn Python the Hard Way a week ago. Im currently at exercise 21, a lot of it makes sense but there is something I just can't understand from ex 21.

Here is the code:

def add(a, b):
    print "ADDING %d + %d" % (a, b)
    return a + b

def subtract(a, b):
    print "SUBTRACTING %d - %d" % (a, b)
    return a - b

def multiply(a, b):
    print "MULTIPLYING %d * %d" % (a, b)
    return a * b

def divide(a, b):
    print "DIVIDING %d / %d" %(a, b)
    return a / b


print "Let's do some math with just functions!"

age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)


# A puzzle for extra credit, type it in anyway.
print "Here is a puzzle."

what = add(age, subtract(height, multiply(weight, divide(iq, 2))))

print "That becomes: ", what, "Can you do it by hand?"
This is what the terminal shows me:

Let's do some math with just functions!
ADDING 30 + 5
SUBTRACTING 78 - 4
MULTIPLYING 90 * 2
DIVIDING 100 / 2
Age: 35, Height: 74, Weight: 180, IQ: 50
Here is a puzzle.
DIVIDING 50 / 2
MULTIPLYING 180 * 25
SUBTRACTING 74 - 4500
ADDING 35 + -4426
That becomes:  -4391 Can you do it by hand?
I don't understand why: "ADDING 30 + 5", "SUBTRACTING 78 - 4", "MULTIPLYING 90 * 2" & "DIVIDING 100 / 2", shows before print.

After we define the functions in the beginning we print: "Let's do some math with just functions!"

Then we create some variables with the functions we created:
age = add(30, 5)
height = subtract(78, 4)
weight = multiply(90, 2)
iq = divide(100, 2)

But they are just defined not called. In the functions there is a print and a return. But the print for all the functions shows and then the return show in: print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq) Why is that ?

Thank you for your help Smile
Reply
#2
(Sep-09-2017, 12:12 PM)pyth31 Wrote: Im very new to programming and started Learn Python the Hard Way a week ago
We are not to happy with Learn Python the Hard Way,not gone say more about that.

It would be better if you used his updated(after a lot noise) Learn Python the Hard Way for Python 3
Take a look at Python 3.6 and pip installation under Windows or for Linux Python 3 environment.
Reply
#3
The def statement is where the functions are defined. For age = add(30, 5) you say it's defined but not called. But it is called. That's how you call the add function: with parameters in parentheses. That causes the add function to print it's message, and the return value is stored in age. Then that age variable is used in the print statement using the results. The % operator for strings allows for inserting values into strings. The first %d in the string is replaced by the first value in the tuple. The 'd' in '%d' indicates the format (digits). Then the second %d is replaced by the second value in the tuple, and so on.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
(Sep-09-2017, 01:16 PM)ichabod801 Wrote: The def statement is where the functions are defined. For age = add(30, 5) you say it's defined but not called. But it is called. That's how you call the add function: with parameters in parentheses. That causes the add function to print it's message, and the return value is stored in age. Then that age variable is used in the print statement using the results. The % operator for strings allows for inserting values into strings. The first %d in the string is replaced by the first value in the tuple. The 'd' in '%d' indicates the format (digits). Then the second %d is replaced by the second value in the tuple, and so on.

Thank you for your answers. So for my understanding:

When the arguments in the parentheses is called in the stand alone print line:

print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq) <-- stand alone print line

Lets say "age" variable is called it first prints its own line outside the stand alone print line (<-- this was causing me problems). "ADDING 30 + 5" then puts the result (return) in the print age, weight, height & iq stand alone line.

Then it moves on to the next argument in the stand alone print line and does the same.
Reply
#5
No. age = add(30, 5) calls the add function with the parameters 30 and 5. The add function then prints 'Adding 30 + 5', and then adds 30 and 5 and returns the value 35. The value 35 is then assigned to the age variable, although it is perhaps more precise to say that the age label is attached to the value 35. Then in the big, stand alone print statement, the age value is used. The age variable is not called. It is an integer, and integers are not callable. It is just used to reference the value 35, which is then put in the string statement where the first %d is.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
The modern way to write this is:
print('Age: {}, Height: {}, Weight: {}, IQ:{}'.format(age, height, weight, iq))
Reply
#7
(Sep-09-2017, 04:50 PM)ichabod801 Wrote: No. age = add(30, 5) calls the add function with the parameters 30 and 5. The add function then prints 'Adding 30 + 5', and then adds 30 and 5 and returns the value 35. The value 35 is then assigned to the age variable, although it is perhaps more precise to say that the age label is attached to the value 35. Then in the big, stand alone print statement, the age value is used. The age variable is not called. It is an integer, and integers are not callable. It is just used to reference the value 35, which is then put in the string statement where the first %d is.

That was the way I wanted to explain it, but I guess I wasn't correct enough
Reply


Forum Jump:

User Panel Messages

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