GUESS AND CHECK - LEARNING TO CODE WITH PYTHON Ep 5

in #esteem6 years ago (edited)

Untitled-1-01.png

Hello friends, this is a blog about my programming journey, I am basically teaching myself how to code. I believe if I have a formal documentation about my progress, I will be less complacent, in other words, I am also learning by teaching and revision.

banner.png

Guess and check is a common math method that can also be used to solve a vast number of problems in programming, in scenarios when the result in unknown, a guessing loop can be initiated and tested against the conditional problem, once the result has been found through a right guess the condition becomes satisfied and the problem is solved.

Although anybody can make a guess, in order to be able to solve our problem we have to make educated guess not wild guesses, meaning: It has to make sense to be a problem, an educated guess is made by checking and observing the conditions of the problem.

question-mark-1872665_960_720.jpg

Let us use the guess and check algorithm to solve a problem, we will find the perfect cube root of any whole number entered by the user.

sol.jpg

The python syntax for cube is [number **3 ], assuming that we are looking for the cube of the variable(number), if we want to find a square we can equally use [number **2], but in this solution we will the using the [number **3] formula as a condition to solve our problem. That is the formula to test our educated guess

first we write a print statement to instruct the user on the what to do.
1.jpg

The data entered by the user is stored the num variable
2.jpg

The while-statement starts a loop, a while-loop should also have a counter, the counter is basically counting the number of times that the while-code-block runs.
3.jpg
For the next number of lines, let's take a look Inside the while-code-block first, inside the while-loop; the counter variable(count) is increased by the value of 1 per loop, in each loop, the counter is also tested against our while-condition [count ** 3] < num.
4.jpg
The while loop is initiated with our check-condition [count ** 3] < num, The while-condition simply means that each value of count is raised to the power of three(cube) and tested against the whole-number(num) entered previously by the user.

Explaining the condition a little further, let's assume that the user enters the whole number(27), the while-code-block will keep increasing the value of count by 1 and testing it against the value of num(27). i.e: when count is 1, it is raised to the power of 3 [1 ** 3], which is equal to 1, then it tests the result if it is less than num(27), if true, it runs the while-loop-code-block again increases count by 1 and tests it again.

As the count keeps increasing and gets to 3, it tests it, the cube of 3 is equal to 27, is (27) less than num(27)? the answer is no, so it exits the loop and moves to the next line.

The next line of codes, consists of If-conditional statements, which further tests our educated guess
5.jpg
The if statements tests if the condition is true, if true, it runs the if-code-block which consists of the print-statement, for instance, after the while-loop exits, the if-statement tests if the cube of the count(3) is equal to the user's input(27), if true, it prints count(3) as the cube root of num(27). if false, it moves on to the next line without running the if-code-block.6.jpg
It is traditional for a If-statement to have an else-statement, in case, the if-condition is false, for instance, let's say the user previously entered the whole-number(28), since 28 doesn't have a perfect-cube root, it therefore, runs the else-code-block.

If you run the program, you get something like this
7.jpg
If you entered a number that doesn't have a perfect cube root, you get something like this.
8.jpg

banner.png
In order to enable our program to accept negative integers/whole-numbers from the user, we can just edit our code and add the abs() function to wherever we see the num variable. we can edit our while and if statements like this
9.jpg

10.jpg

If we run it again and input the whole-number(-27), we get something like this
11.jpg
banner.png

images
That is all for today, thanks for visiting my blog


Sort:  

I don't want to state the obvious, but in order to reduce the lines of code you write, you could have also commenced your file with the following, making the print line superfluous:

num = int(input('What perfect cube root do you want? '))

This will accomplish the same thing but with less code.

Thanks for this post. I really enjoyed reading it.

Thanks for your input, less lines are indeed much better. I appreciate you passing by.

As a follower of @followforupvotes this post has been randomly selected and upvoted! Enjoy your upvote and have a great day!

Me i want to learn oo

lol. we can learn together

Am learning python too, but not really consiatent with it, i plan to do more. This is motivation to me, thanks

you are welcome bro. one step at a time.

Wow! Way back in the day, my dad taught himself MBasic (and taught me in return), and I thought that hurt my brain. Python looks beyond brain hurty, but it seems like you've got a good grasp on it (at least, I think you do...lol), so I wish you continued success on your coding journey. Good idea to blog about it too. 😊

Yes, it does takes some getting used to. but it is a good skill to acquire.

Very true. 😊

Hey @abmakko

Teaching yourself is quite impressive. Nice job.

Gaz

Thanks for the encouragement

Coin Marketplace

STEEM 0.26
TRX 0.20
JST 0.038
BTC 95099.53
ETH 3571.49
USDT 1.00
SBD 3.82