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

- 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>):     ...

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

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