Project 3: Old vs New
Hello! Welcome to my new project, which focuses on making a new Guess the Number code. It's the same program but the code is shorter! Makes coding life a lot easier if you have fewer lines to type.
As a recap, have a look at Project 2! Below is the original code for Guess the Number:
If you prefer to view the old Guess the Number code in PDF format, click here!
In this post, I would like to introduce "or":
"or" is an operator that means "if A or B apply, execute X". Have you noticed that there are some conditions that lead to the same outcome in the original code? That is a valid way of making code, but if you have to make a code that has hundreds or thousands of lines, this can not only feel repetitive and annoying, but it can also make it harder to review your code for mistakes... This is when operators like "or" can be useful, to reduce this repetitiveness as much as possible.
The code below is the new version of the Guess the Number code where "or" is used for conditions that lead to the same outcome:
You may, or may not, have noticed another difference between the old and new codes. The new code also has a function inside another function for y. This is called a "nested function". If you simply want the user to be prompted to enter a number, and want that same number to be recognized as a number by python, the nested function allows both the prompting and recognition to be done in one line instead of keeping the commands in separate lines.
The new code includes "or" and a nested function. This allowed the Guess the Number code to be shortened from 36 lines to 16 lines! That's over half the number of lines gone!!
BUT!!! The most important question: Was it worth shortening the code? Did it affect the quality of the program itself?
The answer: NOTHING TO WORRY ABOUT 😌😎
In this case, the program ended up being pretty much the same as the original code. So I managed to make pretty much the same program but with less than half the number of lines! 🎉🎉🎉
Below is the resulting outcome from the new code:
I hope you enjoyed my new post and hopefully it has helped write shorter codes that is just as useful! If you have any suggestions for future posts, or you know better ways to improve my codes, let me know!
P.S.
Did you know that computers have an in-built snipping tool? It has been a life saver when writing this post so now I don't have to go through processing the images as I used to for the previous posts 😅 For my friend who let me know that this tool exists, you know who you are, THANK YOU 💖 For my readers, I hope you find the new image formats easy to look at, and let me know if you have any issues 👌
No comments:
Post a Comment