Since we're building a scientific calculator, and we want to have access to more advanced math functions, like logarithms or trigonometric functions, we must first import a module that can help us achieve these goals. And this is the math modules. So before anything else inside this program, we should type in import math. Next, if you remember the lecture on while loops A while back, I mentioned that you can use a while loop to build an interactive menu, where the user can select a value and execute a piece of code, then return to the main menu. Now it's time to make this a reality and use a while loop for our menu. This loop if used as while true, keeps running until we choose to quit, thus keeping our program alive after performing each math operation.
So the first task we should solve is building the user menu you've just seen in the previous lecture. This means that as long as the expression In the while loop is evaluated as true, and it always is, since we have, while true, then the program should display the menu to the user, and then ask him to choose an option. Therefore, the first thing we should have under the wild statement is a print function, printing the user menu with each option on a separate row for better readability. Also, we must have a prompt printed out, that will allow the user to input a value and then store that value using a variable. And what better way to do this is they're then to use the input function you've learned about at the beginning of this course and storing the string entered by the user. See how some of these concepts we've discussed are becoming useful in the real world of application development.
Pretty cool. So let's start writing this wire loop. First of all the print function, printing the user menu, and then the input function to store the value entered by the user. Okay, so let's analyze this a bit under the wild, true statement. Right here, first we have the print function. And in between its parentheses, we enter each of the 10 options separated by a backslash n for having each option on a separate row.
Next, using the upper variable, we ask the user to enter a value, which is going to be stored as a string. And then we reference that value using the variable. Now let's test this piece of code and paste it in the Python interpreter. So let me copy this and open the interpreter. Okay, now let me paste this first import math. And now let's actually paste only the while loop.
Okay, great. So this looks just like the user menu we need for our program. Of course, at this point, this is just a pretty menu and no functionality lies behind each of these options. whatever value we choose to enter as the prompt, the while loop keeps running and shows the same user menu over and over. Again, in the next lecture, we are going to start adding functionality for each option in the menu. So see you very soon