Class 6 If/elif/else Statement

Today we are going to learn about If/elif/else (pdf=ifelif)statements.

If you do this work:

you will know if statements

elif you partially do the work:

you might sort of know them

else:

you won’t get much out of today’s lecture!

Some reading

If statements
http://www.tutorialspoint.com/python/python_basic_operators.htm
http://www.tutorialspoint.com/python/python_decision_making.htm
http://www.tutorialspoint.com/python/python_if_else.htm
But we need to know Boolean
https://docs.python.org/2/library/stdtypes.html

Video

Don’t forget the quiz on courseworks.

Homework 5

Due Next Class Wednesday February 7, 2024

    1. We are going to try a new website for learning Python. It is Sololearn and here is the link for the Python Course.   https://www.sololearn.com/Play/Python/ .  You will need to make a login for it.
      1. We will start by doing through Basic Concepts and Control Structures.
      2. You can go through and just answer the questions or also try things out.  It is up to you.
      3. I am just trying to get you extra Python Practice.
      4. This is Due on Monday October 10, 2022 (Courseworks had it correct).
      5. I can not track a class.   The picture shows what mine looked like when done.  
      6. Make sure the picture shows your login name and your name on your computer to prove it is yours.
      7. On the assignment in courseworks you will upload the screenshot.

Regular Homework

  1. Go back and find when you plotted the parabola and sin x.  These are really nice graphs that you made.
    1. Use input and if statements to ask the user if they would like to plot sin x or to plot the parabola
    2. If they choose sin x use an input statement to determine the number of pi’s they want to plot and plot the appropriate graph.  This way you can show many pi values.
    3. If they choose a parabola have them enter the a,b,c of the parabola.
    4. For example if I choose sin of x I could then choose 32.1 pi’s to plot.
    5. For example if I choose parabola I could then choose a=1, b=-5, c=0.02
  2.  You should have all your code in one ipython box so I can just run that one box, do my entries, and then see the plots.  if-statements grading rubric
  3. Remember to use input()
  4. To get an integer or float (remember to use them correctly, You need to choose when you think an integer or float is more appropriate) use input like
    1. a=int(input(‘please enter a number as an integer’))
    2. myfloat=float(input(‘please enter a number as a float’))

18 thoughts on “Class 6 If/elif/else Statement

  1. The online coding materials helped me better understand this concept! When working on this assignment, I had forgotten when I should use an integer and when I need a float and this distinction resurfaced when I was working with data for my final project.

    While I think certain details/ oddities of python have to be learned as you go– perhaps it would be helpful to have some lecture about common mistakes just so these ideas are in the back of folks minds when they run into problems.

  2. It was nice that this notebook tied in other concepts we had learned in earlier lessons (graphing, formatting strings, etc.). In that way it was a good review/practice of many concepts. I thought the graphs at the end served as a good visual representation of the the if/else statements – those helped me understand a bit more of how they could be used outside of a numerical setting. It was also nice to get more practice with graphing here. I had struggled some with getting the data loaded, but once resolved the graphing came smoothly. I also found the comments in the notebook very useful; for the most part the more information/context about the code, the better I am able to understand and replicate it. I thought the HW was a pretty direct translation of our code from the day – I wouldn’t change it, for it helped solidify what we did in class.

  3. I think this was my most favorite class so far, and I was amazed by how easily I was able to do the homework which seemed like a daunting task when I looked through it before the class. I even felt comfortable enough to go the extra mile and create a third option of plotting both graphs together on the same plot. I think this class really brought a lot of the concepts we had learnt in previous classes together and all the pieces start to make so much more sense. One thing that I did have trouble with was trying to set the scale, especially when plotting more than one graph on the same axes- where I ultimately resorted to trial and error. I think that is one area where we could use more clarification!

  4. This class was pretty useful and covered all of the basics of if/else statements. For the homework, converting floats and integers to their counterpart within the statements was tricky and should have been prefaced in the packet. The input function is probably my favorite tool that we have used yet, with basic formatting and the ability to draw random numbers from any array or range of numbers.

  5. I appreciated that this class allowed us to develop a foundational understanding of booleans before introducing raw_input. However, I struggled to combine my understanding of raw input and if, elif else statements to make the graphs for the homework assignment. It may be useful to include an additional dataset so that we can practice using booleans and raw_input before starting the homework.

  6. I think this lecture and packet was quite clear! I had a lot of fun messing around with the ‘if/elif/else’ statements, and I think it was a great addition after learning ‘for’ loops. It’s really valuable in the future and I’ve definitely started using it naturally for ater notebooks and my final project. One thing that I might recommend is to let us figure out our own statements given a random dataset; the homework was pretty easy after the packet.

  7. if/elif/else statements was by far one of the most important lessons and functions we learned. I think it was extremely helpful that we learned a simpler, kid-friendly version on code.org because although it took us through very simple puzzles, it helped to solidify my understanding of the concept more than anything else. I think it’s easy to memorize a code and keep copying and pasting it, but once you understand what the code is used for and what it is saying when it reads it, it becomes much easier and actually fun! I would also never forget how I learned it because of the visuals the program provides, I absolutely enjoyed it and am grateful that it helped to grasp my understanding, I am sure it helps students both younger and older than me as well.

  8. I really enjoyed this particular class and had fun generating the raw_input scenarios! Due to how much I enjoyed this particular Python function, it would have been neat to revisit this concept in later lectures. Continuously implementing raw_inputs every once in a while in proceeding lectures could be a nice way of ensuring this concept is not forgotten!

  9. I really enjoyed using raw_input, I think it was the first time I felt like I was really in control of my code and could confidently create something. the interactive element of it was very fun and I spent a while just playing around and making programs for my friends to try out. It would be interesting to learn how to use this with more complex answers and just explore what sorts of things you can do with raw_input, because I imagine there’s more than we covered.

  10. I think using raw input and choosing a type of graph to make was a good way of teaching if statements because I remember using “if _____ == _____:” a lot as the course progressed, and I suppose this is where I learned it. Good intuition on that one.

    It could be helpful to explain how booleans work in basic logic, e.g. “or” denotes an “either/or” statement, hence if only one of them is true then the statement is true. That’s slightly different pedagogy than just telling us to enter the code and predict what the outcome will be. Both work.

    I would have liked to know where raw input could potentially take us. I know there’s only so much you can squeeze into this class, it’s just a thought. It seemed like it could eventually be used in pretty neat ways and that our exposure to it was minimal.

  11. I think this lecture was effective because it was straightforward and interactive. I had issues with the input function, but had no trouble with raw_input, as expected. I don’t think I’ve used raw_input since this class, and am not sure how useful the function is overall, but learning about boolean and if statements has been useful for other assignments.

  12. I understand this exercise was for familiarizing us with raw_input, but it might make an interesting/useful extension task to challenge us to use for loops (the preceding lesson) and if/elif/else statements to write a raw_input that continues prompting the conditions we’ve set for the input are met- I remember trying to figure this out at the outset and it was pretty tricky.

  13. Agreed, the two versions of inputs were rather confusing, but it really allowed me to understand better which should be used for what. So I am not sure whether they should be eliminated altogether.. maybe highlight the distinction even better for the next class? I really enjoyed the class though! If, elif, and else is a really, really useful function!

  14. It would have been more clear to directly use raw_input, rather than experimenting with the input function. I’ve tried it on other systems and read some forums, they always always suggest to never use input.

    • Thanks! I think I am going to change it. It is weird because it depends on the computer and the Ipython program. But it seems like raw_input is more stable. I only learned that also. But I do like the idea of using input. I think it makes it more fun to program.

  15. I thought the lecture was very clear, and I felt like I was able to do the homework with few questions. I enjoyed learning most about the input statement. It was very satisfying to being able to interact with the code that we made.

    • I thought this homework was time-consuming but fairly straight forward based on what we learned in class and has learned up until this point. I do think the one thing that could have been useful is a code.org or sololearn section on floats and integers at this point. Quite honestly, I really didn’t understand the difference when I was writing this code or when one might be appropriate over the other. I think having some more practice with their definitions and uses outside of class would have been really helpful for me.

Leave a Reply