No Cool Computer Science Thing of the Day
Reading Questions
Quiz
Booleans
- The Boolean type has only 2 values,
True
andFalse
Boolean expressions evaluate to Boolean values
print(1 == 2) print(1 != 2) print(1 < 2) print(1 > 2) print(1 <= 2) print(1 >= 2)
Conditionals
- Write code that executes only if a condition is met
Boolean values are useful for specifying the conditions
integer = int(input('enter an integer: ')) if integer == 0: print('You entered Zero') else: print('You did not enter Zero')
- Can also leave off the else
This can be toublesome if a variable is created in the if block
x = 0 if x != 0: y = x print(y)
Graphics User Input
- Graphics module has functions for getting mouse and keyboard input
The ‘wait’ function are blocking functions, the program stops running until user input
gaphics.wait_for_button_press() graphics.draw_oval(0, 0, 100, 100, fill="black")
Can also get the current mouse state
graphics.mouse_x() graphics.mouse_y()
But this isn’t useful unless repeatedly checking the value inside of a loop
for i in range(100): x = graphics.mouse_x() y = graphics.mouse_y() graphics.clear() graphics.draw_oval(x, y, 100, 100, fill="black") graphics.wait()
This runs a fixed number of frames, it would be better to run as long as the user wants
while graphics.window_open(): x = graphics.mouse_x() y = graphics.mouse_y() graphics.clear() graphics.draw_oval(x, y, 100, 100, fill="black") graphics.wait()
Can also get the button state
x = 0 y = 0 while graphics.window_open(): if graphics.button_down(1): x = graphics.mouse_x() y = graphics.mouse_y() graphics.clear() graphics.draw_oval(x, y, 100, 100, fill="black") graphics.wait()
The 1 argument to the button_down function tests if the left mouse button is down