Lesson 5 Slides
Lesson 5 Slides
Lesson 5
Topics
# setup window
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
draw_poly(size, num_sides)
Run the program, → enter dog .
print(user_value.isdigit())
if user_value.isdigit():
print("That's a number")
user_value.isdigit() → conditional
return True or False
: → indented code block coming
Adjust lesson_5_pt_1b.py
if user_value.isdigit():
print("That's a number")
else:
print("That's not a number")
# setup window
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
draw_poly(size, num_sides)
Predict for these scenarios:
valid sides value and valid size value
valid sides value and invalid size value
invalid sides value and valid size value
invalid sides value and invalid size value
Run the code. Did it follow your prediction?
Definite repetition.
def get_number(prompt):
num = input(prompt)
if num.isdigit():
return int(num)
else:
print("Invalid input")
quit()
def get_number(prompt):
num = input(prompt)
if num.isdigit():
return int(num)
else:
print("Invalid input")
quit()
# setup window
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
draw_poly(size, num_sides)
Refactoring code → test that code still works the same
num = input(prompt)
displays prompt
assigns user input to num
if num.isdigit(): → when num only contains numbers execute
return int(num)
converts num value to integer
sends value to main program
ends the function
else: → if num does not contain only numbers execute:
print("Invalid input") → informs user of error
def get_color():
color = input("Fill colour (red, blue, green)?> ").lower()
if color == "red":
return color
elif color == "blue":
return color
elif color == "green":
return color
else:
print("Invalid input")
quit()
import turtle
def get_number(prompt):
num = input(prompt)
if num.isdigit():
return int(num)
else:
print("Invalid input")
quit()
def get_color():
color = input("Fill colour (red, blue, green)?> ").lower()
if color == "red":
return color
elif color == "blue":
return color
elif color == "green":
return color
else:
print("Invalid input")
quit()
# setup window
screen = 500
window = turtle.Screen()
window.setup(screen, screen)
Complete exercises 1 to 3
Tutorial 2:
While Loop
Two types of loops → different types of iteration
def get_number(prompt):
num = input(prompt)
if num.isdigit():
return int(num)
else:
print("Invalid input")
quit()
if guess == number:
print("Correct!")
else:
print("Incorrect. The number was", number)
number = random.randint(1,100)
random.randint(1,100) → random integer between 1 and 100
Operator Meaning
== checks if two values are the same (equal to)
!= checks if two values are not the same (not equal to)
> checks if the left value is greater than the right value
< checks if the left value is less than the right value
>= checks if the left value is greater than or equal to the right value
<= checks if the left value is less than or equal to the right value
Not a good game → one-in-one-hundred chance of winning.
What kind?
def get_number(prompt):
num = input(prompt)
if num.isdigit():
return int(num)
else:
print("Invalid input")
quit()
if guess == number:
print("Correct!")
else:
print("Incorrect. Try again")
prints Correct!
continues to ask for guesses
guess = 0
def get_number(prompt):
while True:
num = input(prompt)
if num.isdigit():
return int(num)
else:
print("Invalid input")
import random
def get_number(prompt):
while True:
num = input(prompt)
if num.isdigit():
return int(num)
else:
print("Invalid input")
guess = 0
Complete exercise 4