Re-Learning Python - A Journey to Remember!
I haven't coded in python for a long time, then when I came back, I just realized that I forgot almost everything in python! So I decided to learn things again, from the very beginning (the beginning is the end and end is the beginning!). But this time I used the CLASSIC method. Yes, I learned from the books.
I love to introduce myself as a "self-taught" programmer, which means I have no academic experience in this sector. Learned this whole code thing from websites like SoloLearn and KhanAcademy. But this time I wanted something really different, so I grabbed some coding books, which are not even written in English.
Yes, I used these books to re-learn everything, and had a great journey! These are actually some of the most popular programming books written in Bengali. If you are familiar with the Bangla language, you can use these books to start your programming journey. Click here to buy these books.
While studying the "while" loop, stopped at an example, a simple program to print the multiplication table of a certain number. The given code was very straightforward,
n = input("Please enter a positive integer: ")n = int(n)m = 1while m <= 10:print(n, "X", m, "=", n*m)m += 1
Nice code indeed. But I wanted to go to a higher level. When I was young, I used to own a pencil box that had 10 multiplication tables printed on the backside. That info helped me do a lot of cheating on the tests. So, from that gratitude, I wanted to print that whole thing. First I wanted to think about the problem, but then I realized that my brain is so small! So I wrote it down to deal with it.
Problem:
I need some output like:
n X m
1 X 1
1 X 2
1 X 3
.
.
.
1 X 9
1 X 10
2 X 1
2 X 2
2 X 3
.
.
.
.
10 X 9
10 X 10
Attempt 1:
So It's easy! I need to make a loop and increase the m and n value from time to time. I was obvious as hell, so I coded fast like those fancy hackers in movies,
m = 1n = 1while m <= 10:if n <= 10:print(n, "X", m, "=", n*m)m += 1n += 1
But! Once some coding legend(!) said, "If you are obvious before even running your code, I'm pretty sure your code is wrong." Yes. Thad did print something, but that was like this.
OUTPUT:
1 X 1 = 1
2 X 2 = 4
3 X 3 = 9
.
.
.
10 X 10 = 100
So I have a look at the code and saw the problem. Just change some blocks and done!
IS IT?
Attempt 2:
m = 1n = 1while m <= 10:print(n, "X", m, "=", n*m)m += 1if n <= 10:n += 1
Yes, legit speaking, it was my second attempt! How stupid am I!
Yes, the output was the same. So this time I looked at the code a bit more closely. And felt like, I found the real solution to my problem.
The code was not that simple like my thoughts! Here the value of m is increasing in every iteration. When this value will reach 11, we have to reassign the value of m to 1 and increase the value of n by one each time m reaches 11. A bit complex but it works.
Attempt 3:
n = 1m = 1while m <= 11:print(n, "X" , m , "=", n*m)m += 1if m == 11:m = 1n += 1
Well, is it the solution? Can this code print the thing? NO!
This code works good, but doesn't stop! We have to stop it.
FINAL CODE:
n = 1m = 1while m <= 11:print(n, "X" , m , "=", n*m)m += 1if m == 11:m = 1n += 1if n == 11:break
So that is the final solution of the simplest problem, my dumb brain took 3 attempts to just solve such an easy problem!
I hope you had fun reading the article. A follow or comment will be much appreciated!
Use the Promo Code: MATHNIT for Free Shipping!
No comments: