Python Forum
Function encapsulation - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Function encapsulation (/thread-32109.html)



Function encapsulation - Oldman45 - Jan-21-2021

I need help please.
I am trying to learm Python and work with Python 3.83.5 using How To Think Like A Computer Scientist as a guide. I am at Chapter 7 Encapsulation and have the following working function?

n = 3
def print_multiples(n):
    for i in range(1, 7):
        print(n * i, end="   ")
    print()
print(print_multiples(n))
The next stage is (quote) 'By now you can probably guess how to print a multiplication table — by calling print_multiples repeatedly with different arguments. In fact, we can use another loop:
1 for i in range(1, 7):
2 print_multiples(i)
Notice how similar this loop is to the one inside print_multiples. All we did was replace the print function with a function call'

Now I have tried to replace each print statement in the function print_multiples(n)but cannot get the function to work. Can anyone please advise what to do.

The output of this program is a multiplication table:
1 2 3 4 5 6
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
6 12 18 24 30 36


RE: Function encapsulation - spaceraiders - Jan-21-2021

What isn't working about it exactly? You need to specify these things when asking a question on here.


RE: Function encapsulation - BashBedlam - Jan-21-2021

Quote:Now I have tried to replace each print statement in the function print_multiples(n)but cannot get the function to work. Can anyone please advise what to do.
You misunderstood the instructions. The print statement that you want to replace is the one on line 6, like this :

n = 3
def print_multiples(n):
    for i in range(1, 7):
        print(n * i, end="   ")
    print()

for i in range (1, 7) :
	print_multiples(i)



RE: Function encapsulation - Oldman45 - Jan-22-2021

Hello BashBedlam

Thanks for the prompt response - really helpful. Smile My stupidity I guess, as I clearly did not read the instruction properly. I was inserting just the print_multiples(i) instead for the whole for statement! Sad

Thanks again


RE: Function encapsulation - Oldman45 - Jan-22-2021

Hello spaceraiders

Thanks for a prompt reply. In answer to your question my code would not execute. However, BashBedlam has solved the issue for me - it was down to me not reading the instructions correctly. Anyway thanks for your interest.