Skip to content

Commit aadff3f

Browse files
author
Jennifer Yang
committed
exercises for warm up
1 parent 9497de4 commit aadff3f

8 files changed

+268
-0
lines changed

warmup/Animation_No.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from tkinter import *
2+
import random
3+
4+
tk = Tk()
5+
canvas = Canvas(tk, width=500, height=400)
6+
tk.title("Drawing")
7+
canvas.pack()
8+
9+
canvas.create_line(0,0, 500, 400)
10+
canvas.create_rectangle(0, 100, 200, 150, fill="blue")
11+
canvas.create_oval(100, 100, 150, 150, fill="red")
12+
canvas.create_line(500, 400, 10, 10)
13+
canvas.create_oval(30, 30, 150, 150, fill="yellow")
14+
canvas.create_rectangle(90, 90, 200, 200, fill="green")
15+
canvas.create_polygon(10, 60, 110, 160, 210, 260, fill="purple")
16+
17+
colorlist = ["purple", "green", "yellow", "blue", "orange"]
18+
19+
canvas.create_oval(10, 10, 100, 100, fill=colorlist[0-4])
20+
21+
for i in range(100):
22+
x1 = random.randrange(500)
23+
y1 = random.randrange(400)
24+
x2 = random.randrange(500)
25+
y2 = random.randrange(400)
26+
canvas.create_rectangle(x1, y1, x2, y2, fill=colorlist[0-4])
27+
28+
canvas.mainloop

warmup/Animation_Python.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
from tkinter import *
3+
import random
4+
import time
5+
6+
WIDTH = 800
7+
HEIGHT = 600
8+
9+
#I am making a canvas 500 pixels wide and 400 pixels tall
10+
#I named the drawing "Drawing"
11+
tk = Tk()
12+
canvas = Canvas(tk, width=WIDTH, height=HEIGHT)
13+
tk.title("Drawing")
14+
canvas.pack()
15+
16+
#This is making a variable telling the computer a ball is an orange circle with
17+
#a radius of 25 pixels
18+
co = "orange"
19+
ball = canvas.create_oval(10, 10, 60, 60, fill=co)
20+
21+
#The first thing in the move command is what you want to move, then a
22+
#number for how many pixels to the x direction, then a number for how many pixels
23+
#you want to move it in the y direction
24+
xspeed = 4
25+
yspeed = 5
26+
colorlist = ["green", "orange", "blue", "purple", "red", "yellow"]
27+
28+
while True:
29+
canvas.move(ball, xspeed, yspeed)
30+
pos = canvas.coords(ball)
31+
if pos[3] >= HEIGHT or pos[1] <=0:
32+
yspeed = -yspeed
33+
34+
if pos [2] >= WIDTH or pos[0] <=0:
35+
xspeed = -xspeed
36+
tk.update()
37+
time.sleep(0.01)
38+
39+
tk.mainloop()
40+
#Lol spam

warmup/Ball_2.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from tkinter import *
2+
import random
3+
import time
4+
5+
WIDTH = 800
6+
HEIGHT = 600
7+
8+
tk = Tk()
9+
canvas = Canvas(tk, width=WIDTH, height=HEIGHT)
10+
tk.title("Drawing")
11+
canvas.pack()
12+
13+
class Ball:
14+
def __init__(self, start_x, start_y, xs, ys, fill_color):
15+
self.shape = canvas.create_oval(start_x, start_x, start_y, start_y, fill=fill_color)
16+
self.xspeed = xs
17+
self.yspeed = ys
18+
19+
def move(self):
20+
canvas.move(self.shape, self.xspeed, self.yspeed)
21+
self.pos = canvas.coords(self.shape)
22+
if self.pos[3] >= HEIGHT or self.pos[1] <=0:
23+
self.yspeed = -self.yspeed
24+
if self.pos [2] >= WIDTH or self.pos[0] <=0:
25+
self.xspeed = -self.xspeed
26+
27+
newball = Ball(10, 60, 20, 18, "purple")
28+
didiball = Ball(90, 120, 10, 9, "blue")
29+
niuniuball = Ball(20, 80, 14, 15, "green")
30+
31+
while True:
32+
newball.move()
33+
didiball.move()
34+
niuniuball.move()
35+
tk.update()
36+
time.sleep(0.01)
37+
38+
tk.mainloop
39+
40+
#Lol spam

warmup/Game_Python.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
hum = int(input("How many cars are in the parking lot?"))
2+
if hum > 15:
3+
print("Oh no! Too many!")
4+
elif hum < 1:
5+
print("The parking lot is empty!")
6+
else:
7+
print("Ok then.")
8+
9+
heythee = int(input("How many teeth did you lose?"))
10+
if heythee > 25:
11+
print("Impossible!")
12+
elif heythee < 1:
13+
print("You will probably lose teeth as you grow older.")
14+
else:
15+
print("Wonderful!")
16+
17+
gee = int(input("How many days does it take Earth to make a full ratation around the sun?"))
18+
if gee > 365:
19+
print("You lost.")
20+
elif gee < 364:
21+
print("You lost.")
22+
else:
23+
print("Yes! Good job!")
24+
25+
tummy = int(input("How many times do you brush your teeth a day?"))
26+
if tummy < 2:
27+
print("Try brushing more often!")
28+
elif tummy > 4:
29+
print("That's unnecessary.")
30+
else:
31+
print("Good job!")
32+
33+
total = hum + heythee + gee + tummy
34+
if total > 400:
35+
print("Sorry, but you have lost the quiz. Try again later.")
36+
elif total < 385:
37+
print("Too bad you lost!")
38+
else:
39+
print("Good job! You won!")
40+
41+
print("Your total was", total)
42+
43+
44+
45+
46+
47+

warmup/Objects.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from tkinter import *
2+
import random
3+
import time
4+
5+
WIDTH = 1000
6+
HEIGHT = 900
7+
8+
tk = Tk()
9+
canvas = Canvas(tk, width = WIDTH, height = HEIGHT)
10+
tk.title("Bouncing Ball")
11+
canvas.pack()
12+
13+
class Ball:
14+
def __init__(self, start_x, start_y, xspeed, yspeed, fill_color):
15+
self.shape = canvas.create_oval(start_x, start_x, start_y, start_y, fill=fill_color)
16+
self.xspeed = random.randrange(2, 15)
17+
self.yspeed = random.randrange(1, 13)
18+
19+
def move(self):
20+
canvas.move(self.shape, self.xspeed, self.yspeed)
21+
self.pos = canvas.coords(self.shape)
22+
if self.pos[3] >= HEIGHT or self.pos[1] <=0:
23+
self.yspeed = -self.yspeed
24+
if self.pos [2] >= WIDTH or self.pos[0] <=0:
25+
self.xspeed = -self.xspeed
26+
27+
balls = []
28+
for i in range(100):
29+
balls.append(Ball(60, 130, 6, 8, "blue"))
30+
31+
while True:
32+
for ball in balls:
33+
ball.move()
34+
tk.update()
35+
time.sleep(0.01)

warmup/Python_Number_Guessing_Game.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import random
2+
3+
secret = random.randrange(1, 101)
4+
5+
hm = 0
6+
tries = 0
7+
8+
9+
while hm != secret:
10+
try:
11+
hm = int(input("Guess a number between 1 and 101!"))
12+
except:
13+
print("INVALID INPUT")
14+
continue
15+
16+
tries = tries + 1
17+
18+
if hm > secret:
19+
print("Too high!")
20+
21+
elif hm < secret:
22+
print("Too low!")
23+
24+
else:
25+
print("You got it!")
26+
27+
print("The number was", secret)
28+
print("You took", tries,"tries.")
29+
30+
31+
32+
33+

warmup/Rock_Paper_Scissors.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import random
2+
3+
moves = ['rock', 'paper', 'scissors']
4+
player_wins = ['paperrock', 'scissorspaper', 'rockscissors']
5+
6+
while True:
7+
8+
player_move = input("Your move:")
9+
if player_move == 'q':
10+
break
11+
computer_move = random.choice(moves)
12+
13+
if player_move == computer_move:
14+
print("Tie Game!")
15+
16+
elif player_move + computer_move in player_wins:
17+
print("You won!")
18+
19+
else:
20+
print("You lost. ")
21+
22+
print("The computer played:", computer_move)
23+
print("You played", player_move)
24+
25+
26+
27+

warmup/Secret_Code.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Caesar Cypher shifts a letter 13 spaces over
2+
#The commom alphabet is called cleartext
3+
4+
alpha = "abcdefghijklmnopqrstuvwxyz"
5+
6+
def encrypt(cleartext):
7+
cyphertext = ""
8+
for char in cleartext:
9+
if char in alpha:
10+
newpos = alpha.find(char) + 13 % 26
11+
cyphertext += alpha[newpos]
12+
else:
13+
cyphertext += char
14+
return cyphertext
15+
16+
cleartext = input("Cleartext: ")
17+
cleartext = cleartext.lower()
18+
print(encrypt(cleartext))

0 commit comments

Comments
 (0)