Skip to main content

- If structure in Python -


Decision Control Structures:

The control structures help in making decisions when a program is being executed. 

if Statement :

Depending on certain conditions, few statements are executed. 

if-else Statement :

Depending on a condition;( if true or false), statements are executed. 


Elif :
It’s a combination of else followed by if.

 We all have made many decisions in life. (Here, decisions do not mean a big decision.)Most of our decisions depend on different conditions..isn’t it?

some situations in which you had to make a decision?

 ● What to order in a restaurant? 

● What to do in my free time? 

● Which movie to watch? 

● Which subject to take an elective? 

● During exams, which question to attempt first? Depending on where you are going, the dress you select to wear will also change ..isn’t it? So let's try an activity. Select the dress, according to a given condition.

1. If going to a party

 2. If going out for playing 

3. If going swimming 

4. If it's cold outside 

5. If going to school So here depending on the condition, you are deciding what to wear. Similarly in coding also, we have to make certain decisions or depending on certain conditions, some processing has to be done.




Here is a sample flow on how programs are executed. 
Fig 1: Statements in code are executed sequentially in the order they appear. (all the programs done so far are sequential)
Fig 2:Depending on certain conditions, few statements are executed. If the condition is false those statements are not executed.
Fig 3: Certain sets of statements are executed if the condition is true and certain sets of statements are executed if the condition is false.






Let's try to apply whatever we have learned so far, in buying ice cream. Let’s see how it is going to help us. You have finished your exams and your mother has promised, if you get above 70% she will take you to your favorite place -Baskin Robbins.
Write a program to check this condition. 




Modification of the above code, to include what to do if the student didn’t score above 70.


Depending on the condition, statements are getting executed. Also, the last statement is being executed irrespective of condition. 


Results are out and you found that you have scored above 85%. You get Rs 100/- from your mother. Now the difficult part comes - which ice cream to buy ?!!!. Butterscotch Ribbon(2nd choice) and Melted Chocolate Fudge(1st choice) are your favorites. You decide to check the cost of ice cream and buy it. 

Write a program to get the cost of ice creams and print which one to buy.


Baskin Robbins has developed a Kiosk to help the customers in buying their different ice cream pack options. Customers can enter the no of persons and it will suggest which pack to go for. 

Let's Write a program to simulate this. 

1 -Small or Regular 

2- Double

 3 or 4 - Family Pack

 5 or 6 -Happiness Pack 

>6 - Print the entire options and ask them to select 
















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  Usi...

- Object Oriented Programming -

In OOP, we can represent an entity using a class.  A class bundles data (features/properties of entity - color, height, width) and functionality (movement, expression) together.  An instance of a class is known as an object.  Which is the only cartoon series that had predicted that Trump would be elected as the president of the USA and also predicted the coronavirus spread?   Answer: Simpsons.  Simpsons is a very popular animated show. You can build something similar with python.  But have you ever wondered, how Simpsons look the same in all the series? Only his movement looking and, expression change but his main features like color, height, and face look exactly the same all the time.  This is because we create a ‘blueprint’ of Simpsons and whenever we have to simulate him on screen we use this blueprint. This keeps a record of all the features of Simpsons and recreates it whenever we want.  This is where Object-Oriented Programming(OOP) h...

-List Slicing in Python-

 List Slicing : Slicing helps in creating a new list from an existing list.  Syntax list_name [start :end:step] Suppose you have a list of Avengers, arranged in the order of your preference.   avengers=[“Iron Man”,”Thor”,”Hulk”,”Spider Man”,”Captain America”,”Black Panther”,”Hawk Eye”,”Vision”,”Black Widow”] Later you decide to split them into a separate list like  favourite=[“Iron Man”,”Thor”,”Hulk”]                                                   less_favourite =[”Spider Man”,”Captain America”,”Black Panther”]                             least_favourite=[”Hawk Eye”,”Vision”,”Black Widow”]  Now, we have 3 lists formed from the main list. Would you want to create these lists again from the beginning? This is where list slicing helps us. Here is the code to  G...