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