Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Food Tracker App and Summary

Wrapping up what you have learned so far, you’ll implement a basic food tracker app using Python’s conditional statements. Afterwards, a quick course summary is given.

Take the Quiz: Test your knowledge with our interactive “Python Conditional Statements” quiz. You’ll receive a score upon completion to help you track your learning progress:

Interactive Quiz

Python Conditional Statements

Test your understanding of Python conditional statements

00:00 High five! You made it all the way to video six. In this video, we’ll put together a food tracker app using all the concepts you’ve learned in this series and then I’ll summarize everything we’ve learned.

00:13 So, let’s get started on the app. Go ahead and open up your console. Here I am in PyCharm and I’m going to open up a Python file to contain our app. In PyCharm, you can do that with a right-click on your project folder and open Python File.

00:34 Let’s call it food_tracker. And now I’ve got a blank Python file to build our app. Let me fullscreen this here. And we’re ready to start our app!

00:51 First things first, let’s print a header for our app.

00:59 Let’s print, like, 20 or 30 of those, then a title.

01:10 I’m going to copy this line and do it again. Let’s give it a blank line here. All right, so the question we’re going to answer with this app is…

01:31 Go back to our cake and ice cream, the most important topic of this video series. 'Its saturday. Should I eat cake and ice cream today?' So, first thing first, let’s get a list of days, let’s call it weekdays.

01:55 We’re going to put the first six days of the week.

02:14 We’ve got Sunday through Friday. We’re going to need a blank list to store our answers. Now we’re going to set up a for loop.

02:31 What this little for loop right here is doing is saying “For each item in our weekdays list, we want something to happen.” In our case, we’re going to ask a question, so let’s set that up.

02:56 And then we want to set up a variable to capture user input from our program. We’ll give a little prompt here.

03:12 We want capital 'Y' or capital 'N' only as our answers. Then we want to take the answer and put it into an if statement.

03:27 If the answer is uppercase 'Y', we’re going to add it to our answers list.

03:42 Lists have a built-in function called .append(), where you can add something to it. And we can print a little feedback here so we know where we are in our program.

04:05 And to actually get our answer, we’re going to use an if/elif/else block. So if len(), length, is a function that will give you the length of any list or iterable.

04:23 If the length of answers is 6, meaning we had six yes answers. Let’s say, “Yep, eat that cake and ice cream.” And then we can check another condition with elif the length of answers is 5.

04:49 You did pretty good, so maybe let’s just lay off the ice cream. And if it’s anything besides 6 or 5, you had a tough week, so we’re not getting cake and ice cream.

05:13 Okay. So, let’s review our app really quick. We print out a header. We have the main question of our app. 'Its saturday. Should I eat cake and ice cream today?' Two lists.

05:25 weekdays is a list of every day of the week. We have an empty list called answers. For each day in our weekdays list, we want to print out a question, "Did you eat healthy on "—that day?

05:39 There is a variable here called answer that stores the input from our user, which will be a capital 'Y' or a capital 'N'. In a real app, you’d want to validate this input from your users somehow and make sure only certain characters are accepted, but this works for our purposes.

05:58 And then we set up our first if statement, if answer == 'Y': we’re going to append it to our answers list. Print a couple of statements, and then if the length of the answers list is 6, do something, elif, 5, do something, or else, no cake and no ice cream. So, let’s run it.

06:22 I’m going to right-click and click Run ‘food_tracker’. Okay, Food Tracker App, Its saturday. Should I eat cake and ice cream today? Did you eat healthy on Sun? Yes.

06:34 Did you eat healthy on Mon? Yes. Did you eat healthy on Tues? Yes. Wednesday? Thursday? Friday? Yeah. Calculating the answer... Yes eat the cake and ice cream! Sweet!

06:49 So, there’s our food tracker app. Let’s jump back into the summary and wrap things up. So, in summary, this video series covered the if statement.

06:58 It’s a way to control the flow of your program based on conditions. We learned about grouping your code in statements with indentation. We learned about branching and assessing multiple conditions with the else and elif clauses.

07:14 And we talked about some common ways to increase readability and tidiness of your code with one-liners, conditional expressions, and the pass statement.

07:24 So, that’s it! Thanks for working through this video series. Please drop a comment and let me know how I’m doing. Have a great one!

Avatar image for RetiringInComo

RetiringInComo on April 28, 2019

Thought you did a great job!! Can’t wait to eat cake and ice cream with my dogs!!

Avatar image for yraheem

yraheem on May 1, 2019

This was a cool series. Would it be asking too much to add the python lambda if statement?

Avatar image for slickrick

slickrick on May 9, 2019

Great course!

Avatar image for juanfelix65

juanfelix65 on May 16, 2019

This was a cool series.Great course!

Avatar image for Sachin

Sachin on May 31, 2019

Liked this course. Building a app under 7 mins was even more awsome.

Avatar image for aradim

aradim on June 8, 2019

You did great!

Avatar image for rklyba

rklyba on June 15, 2019

Thank you for the course. I like it.

Avatar image for Jean Ferreira

Jean Ferreira on Sept. 4, 2019

Best course I’ve done so far here in RealPython, since we made a quick app while learning about If Statements. For sure, this should be the way to go for the next videos.

Avatar image for SyedRehanAhmed

SyedRehanAhmed on Dec. 4, 2019

The app you built by using just if and else is awesome.

Avatar image for kingjay2498

kingjay2498 on April 12, 2020

Great Course!

Avatar image for keyurratanghayra

keyurratanghayra on April 24, 2020

That is swell! Obervation: If I press “N”, this script does not handle that. This one did trick for me. if answer == 'Y': answers.append(answer) else: pass

Avatar image for Cory

Cory on April 28, 2020

Great video series!

Avatar image for Ghani

Ghani on Oct. 15, 2020

Very well done!

Avatar image for dngrant

dngrant on Oct. 29, 2020

Outstanding series.

Avatar image for Greish

Greish on Dec. 3, 2020

Great course! Will do the quiz next :D

Avatar image for deewright

deewright on Dec. 30, 2020

Great course and a lot of fun.

Avatar image for siraekabut

siraekabut on July 16, 2021

Thx. I like your simple food tracker app

Avatar image for tonypy

tonypy on March 3, 2023

Excellent personable style

Avatar image for rwelk

rwelk on Feb. 11, 2024

Never tried one liners, or any conditional expressions in scripts I’ve written. I Enjoyed the lesson and will experiment with these ideas. Thank you!

Become a Member to join the conversation.