Skip to main content

– Sequences, Algorithms and Programs in Python ---

 Before moving forward, Let's learn how to install Python software in your System which is very important for the further understanding of these topics.

Imagine, you are a regular customer of Uncle Sam’s Pizza restaurant, can you help Uncle Sam prepare the menu and take a printout of it? Which application will you use for that? You will type the menu using MS word and then take a printout of it. right?

 So, you know that we can use MS Word to create documents.

Similarly, if you want to write a python program, you need an IDE (Integrated development environment). This IDE helps you to write a python program and then run it so that you can see the result/output of the program. 

Steps to install python/IDE in your local system. (Windows /MAC ) 

Note: Prerequisite for installing python in Windows- Windows 8 or above.

go to this website. 

1. Click on download python as per your OS. Mac users may download the Mac version. Windows users download depending on 32-bit or 64-bit.











2. Run the downloaded .exe file.











3. Select both checkboxes and then click on install now.

4. Allow the installer to make changes to a computer.

5. Wait till the installation completes.

 6. Once installation is complete, you will get a screen like this.










The name of the IDE we are using is IDLE (Integrated Development and Learning Environment.) 

Now let's understand how to write a program in python.

 1. Go to run-> type IDLE->click on it Or Goto the folder where Python is installed->open IDLE.

 2. To write a new code, Click on File->New File.










3. You can enter the code in the white area. To run code written, 

1. Click on Run->Run Module 











2. The python shell will open showing the output.

we have successfully installed python in your system.


Sequence:

The sequence is the correct order of doing a task.

 Let's understand the sequence with a basic example.

Do you know how to make a pizza? Have you ever tried making your own homemade pizza? Let's make one now.

1. Prepare the dough and roll it into a circle.

 2. Put on the pizza sauce, cheese, and toppings.

 3. Put the pizza in the oven.

 4. Bake until the pizza is crispy and golden brown. 

Now tell me, is it possible to skip steps 2 & 3 and arrive at step 4 from step 1 directly. Is it possible to do step 2 before step 1? No right?

When things are arranged in the correct sequence; it is not possible to skip a step or alter the sequence. It has to be followed as it is. Correct ordering is important because if we don't follow the correct order we will not get the correct result.

The same is the case when you are giving instructions to the computer. Each step of the sequence has to be followed as it is. 

Algorithm: 

 An algorithm is the set of instructions given in the correct order(sequence) to complete a task. It can be in English or any other language understandable by the user.

Do you know, just now you came up with the algorithm to make pizza. But do you think, the computer will understand if you give these instructions in English? Correct!! So if you want to give instructions to a computer, it has to be in some programming language understandable by the computer. 

Program:

A program is a set of instructions given to a computer, in some programming language. The algorithm serves as a blueprint to write a program. Alright, now let's watch something to understand this in a better way!

Play Video.

What happens in the video? The dad is not able to make the sandwich following the kid’s instructions, isn’t it? 

And why is that? 

● the sequence of the algorithm wasn't correct and that is the reason that the final result was not what the dad wanted. 

● the algorithm has to be very specific if you need the desired result. So did you enjoy it

Now let’s move on and help Uncle Sam in the restaurant.

Uncle Sam wants to help the customers in all possible ways. So, can you tell me the algorithm to place the order in the restaurant, so that Uncle Sam can display it as a tip?

 ● Go to the counter.

 ● Tell the choice of pizza and quantity required.

 ● The salesman places the order and generates the bill. 

● Make the payment.

● Get balance.

● Wait for your order to be ready Now suppose the restaurant wants the customer to enter their phone number, to maintain a record of the client. Develop an algorithm to validate if the phone number entered is correct or not. 

● Check the length of the phone number entered. It has to be 10. If not 10, the number is not valid 

● Check all the characters in the phone number. All of them should be numbers else it won’t be valid. Note: Looping through the number is not given since the student is not introduced to that concept.

Let’s try to write a program now for Uncle Sam who wants to display pizza-making steps in the kitchen. 




OUTPUT:

















program to print the menu of the restaurant. i.e.

(Welcome to Pizza Hub Today's Special

 ----------------------------------------

 1. Large Pizza Rs. 120

 2. Medium Pizza Rs. 80 

3. Small Pizza Rs. 60

 4. Toppings extra Rs. 10

 5. Extra cheese Rs. 15

 ---------------------------------------- 

Place the order on the counter and wait 10 mins for your order Thanks for giving us a chance to serve you.









Comments

Popular posts from this blog

- Animating characters using pygame -

If you see closely you can see that the mickey mouse animation is made out of a lot of shapes.  But to create something like this we need to make a very complex code. The other way around it is to use an image, other than the basic shapes, pygame also can show images.  Using images :  Steps to use images in my game: > find the image you want to use  > move it to the directory where you have created your code  > import/load image  To load the image we will first create an object and then use the image. load method to add the image to the object.  here, we have created an object called an image.  We then use the "pygame.image.load" to find the file in the directory ocean.png has to be replaced with the filename of your file.  To place the object on a particular point of the screen we use the blit function.  this function means that we are going to add the image onto the screen at position 0,0 mickey mouse is winking  Using text: We can also add text to the pygame, to a

- Functions in Python -

  Function-  ● A function is a named block of code designed to perform a specific task.  ● It takes in some input (optional), processes it, and gives us some output. (optional)  ● A function once defined, can be called many times. eg: function to draw a square, function to calculate average, etc. A function has 2 parts - function definition and function call.  Have you ever used a soft drink vending machine?  You give it some input (the drink you want, money ) and it does some processing inside(checks the amount, checks availability) and gives you the output(the soft drink).  Here, you are not bothered about, how the machine works or from where it gets the soft drink. right? You just give the input and you get the required output. isn’t it?  Functions in programming also behave in a similar way. Function definition: Set of instructions that form the function.  Syntax  def  <function name>(<parameter>):        <statement 1>         <statement 2>        --------  

- Quiz game in Python -

 Game Overview  ● Questions are stored in a file. We will have to read it from the file and store it in a dictionary.  ● 2 points for the correct answer and -1 point for the wrong answer  ● Max no. of questions asked:5  ● Questions will be shuffled once and then asked sequentially.  File Template (for the program)  Each question in the file is stored as <Question> : <Answer> Here ‘:’ acts as a separator between question and answer.  Each line read from the file will be of string data type. ‘:’ helps in separating the answer from the question.  Note: Make sure you add a ‘:’ between question and answer if adding a question manually.  Algorithm  Now that you have seen how the quiz game works, Let's have look at the algorithm for the same.  1. Read the entire file  2. From each line read, get the question and answer a. Add to dictionary  3. Print the rules of the game 4. Shuffle the questions  5. Get each question from the quiz dictionary           a. Ask the question