Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function encapsulation
#1
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
Reply
#2
What isn't working about it exactly? You need to specify these things when asking a question on here.
Oldman45 likes this post
Reply
#3
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)
Reply
#4
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
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Encapsulation codinglearner 2 1,427 Apr-02-2024, 01:26 PM
Last Post: DataScience
  Preserve Encapsulation while Displaying Information QueenSvetlana 13 6,940 Dec-07-2017, 06:13 PM
Last Post: snippsat
  Encapsulation issue iFunKtion 4 3,929 Mar-07-2017, 10:13 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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