Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements

KidsCanCode · Beginner ·🧒 Coding for Kids ·11y ago

Key Takeaways

Teaches input and conditional statements in Python

Full Transcript

welcome back to learning to code with python lesson number seven input and conditional statements so up to now all of our programs have been non-interactive we just run them and sit back to watch what happens but what if you want to be able to ask a question in your program and have the person running it be able to answer well let's find out so I'm starting a new program here and we're going to leave Fred the turtle behind for now because we won't need him for this so first let's ask the person the program the user to tell us their name to do that I'm going to use the input function so the way the input function works is in the parentheses you put the question that you want to ask so this is what the user will see when they run the program and then it'll wait for them to type in a response and whatever response they type in will get saved into the variable name so now that we have the user's name saved in the variable name we can say hello a print function is pretty neat it lets you put more than one item inside the parentheses if you want and it'll print all of them out you just have to put a comma between them so I'm going to print out this part in quotes saying hello and then whatever the VAR variable name is equal to so let's run it and see what happens what's your name there we go right at this point we need to talk about something that the input function does that you need to watch out for here's an example let's say in our program we're going to ask the user what their Agee is so we ask them how old are you okay and when it asks you the question let's say we type in a number okay I'll say a number okay so now that number 30 has been stored in the variable age and we could print out age but here's something that you may not expect what if we said next year you will be and we'll put age + one now you would expect it would print out 30 + 1 but instead you get an error message the error message says it can't convert int to string what does that mean well the variable age if we look at what type it is and type is a way to tell you what kind of data is stored in that variable it's a string anytime you type in something with input it saves it as a string meaning it doesn't think of it as a number it just thinks of it as some string like this but that means that we can't add to it right because that's an error message so we need a way to tell the computer that we want this data that we input to be treated as a number so that we can add one to it we can do that with a function called int int basically takes a string and we'll convert it into a number so if we say int age plus one that's what we wanted so if we were to try our print statement again we need to make sure we just put int before age and that's what we wanted let's ask for a different number now in our program we're going to ask how many ninjas are attacking and remember we want this to be a number so I want to make sure and put the int function around that now I want my program to do something different depending on how many ninjas are attacking if there's more than 50 I want to print out that's too many otherwise we can print out something like we can take them but here's the thing that requires us to do something called a branch or a conditional statement that means we want different commands to run depending on a different circumstance so for example so here's what that looks like we use the command if to indicate we're going to do a branch so this conditional statement will say if ninjas is greater than 50 so the computer is going to make that comparison it's going to compare whatever Ninjas the value of ninjas is and if it's greater than 50 then it's going to execute this block of code that comes after remember just like we did in our for Loops we have a block of code that's indented underneath the for Loop for what commands we wanted it to repeat same thing here so this command print that's too many will only execute if ninjas is greater than 50 if if this comparison right here is false then this command does not get executed so let's try that out so if I say let's put a bigger number first if I say 51 then it prints out that's too many what about if I run it again and this time I'm going to say that there's fewer so there's only 24 attacking I see nothing happened so how do we add a second option well we can use the keyword else so this gives us another block of code that's the alternative case so if ninjas is greater than 50 you should do this but if it's not you're going to do this which is print we can take them so now our program has two paths it can follow depending on the value of ninjas it will either run this print statement or this print statement but only one let's add one more option let's say that we have three branches that we want this program to take if it's greater than 50 that's fine but what about maybe somewhere in between well we can add a third choice by adding L if now L if you can think of as short for else if so it's basically going to do another test we're going to say l if ninjas is greater than 20 and if that's the case then we're going to print maybe we can do it okay so now we have three different paths if ninjas is greater than 50 this happens none of the rest does if ninjas is not greater than 50 then it goes to the next option is it greater than 20 if it is do this and if it wasn't either of these then the last case is this one so let's try that out let's try 18 18 is not greater than 20 so we can take them and we'll try one more and let's say we're somewhere in the middle branching is a key Concept in programming and we'll make use of it quite a lot as we go through this series in our next lesson we'll use some if statements to make a simple game

Original Description

Learning to Code with Python Lesson 1.7: Getting Input and Conditional Statements Support me on Patreon: http://patreon.com/kidscancode Links: * Installing Python: http://kidscancode.org/python-install.html * Asking for help: help@kidscancode.org
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from KidsCanCode · KidsCanCode · 7 of 60

1 Learning to Code with Python: Lesson 1.1 - What is Programming?
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
2 Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
3 Learning to Code with Python: Lesson 1.3 - Variables
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
4 Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
5 Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
6 Learning to Code with Python: Lesson 1.6 - Functions
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
8 Learning to Code with Python: Lesson 1.8 - Number Guessing Game
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
9 KidsCanCode - Patreon Intro Video
KidsCanCode - Patreon Intro Video
KidsCanCode
10 Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
11 Learning to Code with Python: Lesson 1.10 - Secret Codes
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
12 Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
13 Learning to Code with Python: Lesson 2.2 Simple Animation
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
14 Learning to Code with Python: Lesson 2.3: Animating More Objects
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
15 Learning to Code with Python: Lesson 2.4: More Fun with Animation
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
16 Extra: Setting up the Atom Editor for Python
Extra: Setting up the Atom Editor for Python
KidsCanCode
17 Game Development 1-1: Getting Started with Pygame
Game Development 1-1: Getting Started with Pygame
KidsCanCode
18 Game Development 1-2: Working with Sprites
Game Development 1-2: Working with Sprites
KidsCanCode
19 Game Development 1-3: More About Sprites
Game Development 1-3: More About Sprites
KidsCanCode
20 Pygame Shmup Part 1: Player Sprite and Controls
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
21 Pygame Shmup Part 2: Enemy Sprites
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
22 Pygame Shmup Part 3: Collisions (and Bullets!)
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
23 Pygame Shmup Part 4: Adding Graphics
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
24 Pygame Shmup Part 5: Improved Collisions
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
25 Pygame Shmup Part 6: Sprite Animation
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
26 Pygame Shmup Part 7: Score (and Drawing Text)
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
27 Pygame Shmup Part 8: Sound and Music
Pygame Shmup Part 8: Sound and Music
KidsCanCode
28 Pygame Shmup Part 9: Shields
Pygame Shmup Part 9: Shields
KidsCanCode
29 Pygame Shmup Part 10: Explosions
Pygame Shmup Part 10: Explosions
KidsCanCode
30 Pygame Shmup Part 11: Player Lives
Pygame Shmup Part 11: Player Lives
KidsCanCode
31 Pygame Shmup Part 12: Powerups
Pygame Shmup Part 12: Powerups
KidsCanCode
32 Pygame Shmup Part 13: Powerups (part 2)
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
33 Pygame Shmup Part 14: Game Over Screen
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
34 Pygame Platformer Part 1: Setting Up
Pygame Platformer Part 1: Setting Up
KidsCanCode
35 Pygame Platformer Part 2: Player Movement
Pygame Platformer Part 2: Player Movement
KidsCanCode
36 Pygame Platformer Part 3: Gravity and Platforms
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
37 Pygame Platformer Part 4: Jumping
Pygame Platformer Part 4: Jumping
KidsCanCode
38 Pygame Platformer Part 5: Scrolling the Window
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
39 Pygame Platformer Part 6: Game Over
Pygame Platformer Part 6: Game Over
KidsCanCode
40 Pygame Platformer Part 7: Splash & End Screens
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
41 Pygame Platformer Part 8: Saving High Score
Pygame Platformer Part 8: Saving High Score
KidsCanCode
42 Pygame Platformer Part 9: Using Spritesheets
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
43 Pygame Platformer Part 10: Character Animation (part 1)
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
44 Pygame Platformer Part 11: Character Animation (part 2)
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
45 Pygame Platformer Part 12: Platform Graphics
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
46 Pygame Platformer Part 13: Improved Jumping
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
47 Pygame Platformer Part 14: Sound and Music
Pygame Platformer Part 14: Sound and Music
KidsCanCode
48 Pygame Platformer Part 15: Powerups
Pygame Platformer Part 15: Powerups
KidsCanCode
49 Pygame Platformer Part 16: Enemies
Pygame Platformer Part 16: Enemies
KidsCanCode
50 Pygame Platformer Part 17: Using Collision Masks
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
51 Pygame Platformer Part 18: Scrolling Background
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
52 Pygame Platformer Part 19: Wrapping Up
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
53 Gamedev In-depth Topics: 4-way vs. 8-way Movement
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
54 Gamedev In-depth Topics: Time-based vs. Frame-based Movement
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
55 Gamedev In-depth Topics: Non-integer Movement
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
56 Tile-based game Part 1: Setting up
Tile-based game Part 1: Setting up
KidsCanCode
57 Tile-based game Part 2: Collisions and Tilemap
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
58 Tile-based game Part 3: Smooth Movement
Tile-based game Part 3: Smooth Movement
KidsCanCode
59 Tile-based game Part 4: Scrolling Map / Camera
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
60 Tile-based game Part 5: Player Graphics
Tile-based game Part 5: Player Graphics
KidsCanCode

Related Reads

📰
How Creative Characters Like Sprunki Inspire Kids to Learn Coding
Learn how creative characters like Sprunki inspire kids to learn coding through interactive games and animations
Medium · Programming
📰
Computer Applications for Primary School Children: A Fun + Safe Guide for Ages 6-12
Learn how to introduce primary school children to computer applications in a fun and safe way, teaching them essential skills for the future.
Dev.to · Ogunkola Adeola
📰
From 0 to 20 Chapters: My Story‑Driven Rust Book for Kids Now Has a Bilingual Interactive Demo
Create an interactive coding book for kids using Rust, with a focus on storytelling and bilingual support, to teach programming concepts in an engaging way
Dev.to · born1987-ir
📰
How Kids Can Build Fighting Games Stickman in Scratch
Kids can learn coding by building a stickman fighting game in Scratch, developing skills like movement control and collision detection.
Medium · Programming
Up next
Man Builds a Backyard Tiny House and Turns It into a Home Office | Start to Finish by @Elseweyr
AKLA GELEN
Watch →