Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
Skills:
Visual / Block Coding80%
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
2
3
4
5
6
▶
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
KidsCanCode - Patreon Intro Video
KidsCanCode
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
Extra: Setting up the Atom Editor for Python
KidsCanCode
Game Development 1-1: Getting Started with Pygame
KidsCanCode
Game Development 1-2: Working with Sprites
KidsCanCode
Game Development 1-3: More About Sprites
KidsCanCode
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
Pygame Shmup Part 8: Sound and Music
KidsCanCode
Pygame Shmup Part 9: Shields
KidsCanCode
Pygame Shmup Part 10: Explosions
KidsCanCode
Pygame Shmup Part 11: Player Lives
KidsCanCode
Pygame Shmup Part 12: Powerups
KidsCanCode
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
Pygame Platformer Part 1: Setting Up
KidsCanCode
Pygame Platformer Part 2: Player Movement
KidsCanCode
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
Pygame Platformer Part 4: Jumping
KidsCanCode
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
Pygame Platformer Part 6: Game Over
KidsCanCode
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
Pygame Platformer Part 8: Saving High Score
KidsCanCode
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
Pygame Platformer Part 14: Sound and Music
KidsCanCode
Pygame Platformer Part 15: Powerups
KidsCanCode
Pygame Platformer Part 16: Enemies
KidsCanCode
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
Tile-based game Part 1: Setting up
KidsCanCode
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
Tile-based game Part 3: Smooth Movement
KidsCanCode
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
Tile-based game Part 5: Player Graphics
KidsCanCode
More on: Visual / Block Coding
View skill →Related Reads
📰
📰
📰
📰
How Creative Characters Like Sprunki Inspire Kids to Learn Coding
Medium · Programming
Computer Applications for Primary School Children: A Fun + Safe Guide for Ages 6-12
Dev.to · Ogunkola Adeola
From 0 to 20 Chapters: My Story‑Driven Rust Book for Kids Now Has a Bilingual Interactive Demo
Dev.to · born1987-ir
How Kids Can Build Fighting Games Stickman in Scratch
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI