Uploaded by molin guelph

Computer Science Code

advertisement
formatEx2.py
#Giving values
item1 = 2.55
item2 = 3.20
item3 = 4.00
#Calculating total cost
total = item1+item2+item3
#Output result
print("Item one: $%5.2f" %item1)
print("Item two: $%5.2f" %item2)
print("Item three: $%5.2f" %item3)
print("
-------")
print("Total cost $%5.2f" %total)
formatEx3.py
#Giving values
glove=12.89
toque=18.99
scarf=20.00
#Calculations
tax=0.13*(glove+toque+scarf)
total=tax+glove+toque+scarf
#Output result
print("WOSS Gift Shop Receipt")
print("----------------------")
print("\nglove
$%5.2f" %glove)
print("toque
$%5.2f" %toque)
print("scarf
$%5.2f" %scarf)
print("
-------")
print("HST (13%%)
$%5.2f" %tax)
print("
-------")
print("TOTAL
$%5.2f" %total)
--------------------------------------------------------------------#Constants
TAXRATE = 0.13
TAXRECEIPT = "HST (13%)"
TOTALRECEIPT = "TOTAL"
#glove=12.89
#toque=18.99
#scarf=20.00
#Asks user for items
item1 = input("Please enter the name of your first item: ")
item2 = input("Please enter the name of your second item: ")
item3 = input("Please enter the name of your third item: ")
#Asks user for price
price1 = float(input("Please enter the price of your first item: "))
price2 = float(input("Please enter the price of your second item: "))
price3 = float(input("Please enter the price of your third item: "))
#Calculations
tax = TAXRATE*(price1 + price2 + price3)
total = tax + price1 + price2 + price3
#Output result
print("\nWOSS Gift Shop Receipt")
print("----------------------")
print("%-16s$%5.2f" %(item1, price1))
print("%-16s$%5.2f" %(item2, price2))
print("%-16s$%5.2f" %(item3, price3))
print("
------")
print("%-16s$%5.2f" %(TAXRECEIPT, tax))
print("
------")
print("%-16s$%5.2f" %(TOTALRECEIPT, total))
stringEx1.py
#Getting name from user
name=input("Please enter your full name: ")
#Output result
position=name.find(" ")
print(name[position+1:].upper()+", "+name[:position].lower())
print(name[name.find(" ")+1:].upper()+", "+name[:name.find(" ")].lower())
stringEx3
sentence=input("Please enter a sentence: ")
list=sentence.split(" ")
# getting length of list
length = len(list)
# Iterating the index
# same as 'for i in range(len(list))'
for i in range(length):
print(list[i])
print(("X"*number,"\n")*number)
stringEx4
number=int(input("Please enter a number: "))
for i in range(number):
print("X"*number)
functionEx3.py
import math
#Called functions
def slope(x1, y1, x2, y2):
return (y2-y1)/(x2-x1)
def distance(x1, y1, x2, y2):
return math.sqrt((y2-y1)**2 + (x2-x1)**2)
#Asks user for coordinates
firstcoordinate = input("Please enter your first coordinate in the format (x, y): ")
secondcoordinate = input("Please enter your first coordinate in the format (x, y): ")
#Assigning x and y values
x1 = float(firstcoordinate[firstcoordinate.find(",")-1])
y1 = float(firstcoordinate[firstcoordinate.find(" ")+1])
x2 = float(secondcoordinate[secondcoordinate.find(",")-1])
y2 = float(secondcoordinate[secondcoordinate.find(" ")+1])
#Calculations
distance = distance(x1, y1, x2, y2)
slope = slope(x1, y1, x2, y2)
print("The distance between points ("+str(x1)+", "+str(y1)+") and ("+str(x2)+", "+str(y2)+") is
"+str(round(distance,2))+".")
print("The slope between points ("+str(x1)+", "+str(y1)+") and ("+str(x2)+", "+str(y2)+") is
"+str(round(slope,2))+".")
functionEx4.py
#Called functions
def centre(word, fieldsize):
firsthalf = int(fieldsize/2)
secondhalf = fieldsize - firsthalf
return "."*firsthalf+word+"."*secondhalf
#Asks user for string and fieldsize
word = input("Please enter a word: ")
fieldsize = int(input("Please enter a fieldsize: "))
#Output result
print(centre(word, fieldsize))
functionEx5.py
#Called function
def capitals(string):
return
string.count("A")+string.count("B")+string.count("C")+string.count("D")+string.count("E")+string.c
ount("F")+string.count("G")+string.count("H")+string.count("I")+string.count("J")+string.count("K")
+string.count("L")+string.count("M")+string.count("N")+string.count("O")+string.count("P")+string.
count("Q")+string.count("R")+string.count("S")+string.count("T")+string.count("U")+string.count("
V")+string.count("W")+string.count("X")+string.count("Y")+string.count("Z")
#Asks user for sentence
string = input("Please enter a sentence: ")
#Output result
print(capitals(string))
import pygame
pygame.init()
SIZE = (800, 600)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
screen = pygame.display.set_mode(SIZE)
def background():
screen.fill(WHITE)
background()
pygame.draw.line(screen, BLACK, (267, 0), (267, 600), 2)
pygame.draw.line(screen, BLACK, (533, 0), (533, 600), 2)
pygame.draw.line(screen, BLACK, (0, 200), (800, 200), 2)
pygame.draw.line(screen, BLACK, (0, 400), (800, 400), 2)
pygame.display.flip()
pygame.time.wait(1000)
pygame.draw.line(screen, BLACK, (750, 50), (593, 140), 2)
pygame.draw.line(screen, BLACK, (593, 50), (750, 140), 2)
pygame.display.flip()
pygame.time.wait(1000)
pygame.draw.circle(screen, BLACK, (400, 300), 100, 2)
pygame.display.flip()
pygame.time.wait(1000)
pygame.draw.line(screen, BLACK, (210, 450), (50, 550), 2)
pygame.draw.line(screen, BLACK, (50, 450), (210, 550), 2)
pygame.display.flip()
pygame.time.wait(1000)
pygame.draw.circle(screen, BLACK, (667, 500), 75, 2)
pygame.display.flip()
pygame.time.wait(1000)
pygame.draw.line(screen, BLACK, (50, 50), (50, 550), 2)
pygame.draw.line(screen, BLACK, (50, 450), (210, 550), 2)
Super hero
import pygame
pygame.init()
SIZE = (800, 600)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
SKYBLUE = (52, 235, 232)
PURPLE = (180, 51, 255)
screen = pygame.display.set_mode(SIZE)
def background():
screen.fill(GREEN)
pygame.draw.rect(screen, (SKYBLUE), pygame.Rect(0, 0, 800, 250))
background()
pygame.display.flip()
pygame.time.wait(2000)
pygame.draw.circle(screen, (PURPLE), (400, 300), 100)
pygame.draw.ellipse(screen, (PURPLE), pygame.Rect(325, 150, 50, 100))
pygame.draw.ellipse(screen, (PURPLE), pygame.Rect(425, 150, 50, 100))
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(350, 250, 20, 40))
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(425, 250, 20, 40))
pygame.draw.ellipse(screen, (BLACK), pygame.Rect(350, 250, 18, 35))
pygame.draw.ellipse(screen, (BLACK), pygame.Rect(425, 250, 18, 35))
pygame.draw.ellipse(screen, (BLUE), pygame.Rect(350, 250, 16, 30))
pygame.draw.ellipse(screen, (BLUE), pygame.Rect(425, 250, 16, 30))
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(352, 255, 10, 20))
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(427, 255, 10, 20))
pygame.draw.circle(screen, (BLACK), (357, 265), 3)
pygame.draw.circle(screen, (BLACK), (432, 265), 3)
pygame.draw.rect(screen, (PURPLE), pygame.Rect(250, 275, 75, 50))
pygame.draw.rect(screen, (PURPLE), pygame.Rect(475, 275, 75, 50))
pygame.draw.circle(screen, (PURPLE), (350, 400), 30)
pygame.draw.circle(screen, (PURPLE), (450, 400), 30)
pygame.draw.ellipse(screen, (BLACK), pygame.Rect(380, 290, 40, 20))
pygame.draw.line(screen, (BLACK), (370, 330), (430, 330), 2)
pygame.display.flip()
pygame.time.wait(2000)
import pygame
pygame.init()
SIZE = (800, 600)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
SKYBLUE = (52, 235, 232)
PURPLE = (180, 51, 255)
screen = pygame.display.set_mode(SIZE)
#Drawing functions
def background():
screen.fill(GREEN)
pygame.draw.rect(screen, (SKYBLUE), pygame.Rect(0, 0, 800, 250))
def drawbody():
pygame.draw.circle(screen, (PURPLE), (400, 300), 100)
pygame.draw.ellipse(screen, (PURPLE), pygame.Rect(325, 150, 50, 100))
pygame.draw.ellipse(screen, (PURPLE), pygame.Rect(425, 150, 50, 100))
def eyeopt1():
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(350, 250, 20, 40))
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(425, 250, 20, 40))
pygame.draw.ellipse(screen, (BLACK), pygame.Rect(350, 250, 18, 35))
pygame.draw.ellipse(screen, (BLACK), pygame.Rect(425, 250, 18, 35))
pygame.draw.ellipse(screen, (BLUE), pygame.Rect(350, 250, 16, 30))
pygame.draw.ellipse(screen, (BLUE), pygame.Rect(425, 250, 16, 30))
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(352, 255, 10, 20))
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(427, 255, 10, 20))
pygame.draw.circle(screen, (BLACK), (357, 265), 3)
pygame.draw.circle(screen, (BLACK), (432, 265), 3)
def eyeopt2():
pygame.draw.circle(screen, (WHITE), (365, 265), 15)
pygame.draw.circle(screen, (WHITE), (435, 265), 15)
pygame.draw.circle(screen, (BLACK), (365, 265), 12)
pygame.draw.circle(screen, (BLACK), (435, 265), 12)
pygame.draw.circle(screen, (BLACK), (365, 265), 12)
pygame.draw.circle(screen, (BLACK), (435, 265), 12)
pygame.draw.circle(screen, (GREEN), (365, 265), 10)
pygame.draw.circle(screen, (GREEN), (435, 265), 10)
pygame.draw.circle(screen, (BLACK), (435, 265), 5)
pygame.draw.circle(screen, (BLACK), (365, 265), 5)
def eyeopt3():
pygame.draw.circle(screen, (WHITE), (365, 265), 15)
pygame.draw.circle(screen, (WHITE), (435, 265), 15)
pygame.draw.circle(screen, (BLACK), (365, 265), 12)
pygame.draw.circle(screen, (BLACK), (435, 265), 12)
pygame.draw.circle(screen, (BLACK), (365, 265), 12)
pygame.draw.circle(screen, (BLACK), (435, 265), 12)
pygame.draw.circle(screen, (BLUE), (365, 265), 8)
pygame.draw.circle(screen, (BLUE), (435, 265), 8)
pygame.draw.circle(screen, (BLACK), (435, 265), 3)
pygame.draw.circle(screen, (BLACK), (365, 265), 3)
def rectarms():
pygame.draw.rect(screen, (PURPLE), pygame.Rect(250, 275, 75, 50))
pygame.draw.rect(screen, (PURPLE), pygame.Rect(475, 275, 75, 50))
def ellipsearms():
pygame.draw.ellipse(screen, (PURPLE), pygame.Rect(250, 275, 75, 50))
pygame.draw.ellipse(screen, (PURPLE), pygame.Rect(475, 275, 75, 50))
def circlefeet():
pygame.draw.circle(screen, (PURPLE), (350, 400), 30)
pygame.draw.circle(screen, (PURPLE), (450, 400), 30)
def rectfeet():
pygame.draw.rect(screen, (PURPLE), pygame.Rect(320, 380, 60, 30))
pygame.draw.rect(screen, (PURPLE), pygame.Rect(420, 380, 60, 30))
def noseopt1():
pygame.draw.ellipse(screen, (BLACK), pygame.Rect(380, 290, 40, 20))
def noseopt2():
pygame.draw.circle(screen, (BLACK), (400, 300), 10)
def noseopt3():
pygame.draw.polygon(screen, (BLACK), ((390,295), (410, 295), (400, 315)))
def mouthopt1():
pygame.draw.line(screen, (BLACK), (370, 330), (430, 330), 2)
def mouthopt2():
pygame.draw.ellipse(screen, (BLACK), pygame.Rect(360, 320, 80, 20))
#Output
background()
pygame.display.flip()
pygame.time.wait(2000)
drawbody()
pygame.display.flip()
pygame.time.wait(1000)
eyeopt3()
pygame.display.flip()
pygame.time.wait(1000)
rectarms()
pygame.display.flip()
pygame.time.wait(1000)
circlefeet()
pygame.display.flip()
pygame.time.wait(1000)
noseopt2()
pygame.display.flip()
pygame.time.wait(1000)
mouthopt2()
pygame.display.flip()
pygame.time.wait(2000)
pygame.quit()
selectionEx1.py
NORMALTAX = 0.13
CHEAPTAX = 0.08
priceOfMeal = float(input("Enter the price of the meal: "))
if priceOfMeal > 4:
tax1 = priceOfMeal*NORMALTAX
total1 = tax1 + priceOfMeal
print("McD's Receipt")
print("-------------")
print("Meal
$%5.2f" %priceOfMeal)
print("
-------")
print("Tax (13%%)
$%5.2f" %tax1)
print("
-------")
print("TOTAL
$%5.2f" %total1)
elif priceOfMeal <= 4:
tax2 = priceOfMeal*CHEAPTAX
total2 = tax2 + priceOfMeal
print("McD's Receipt")
print("-------------")
print("Meal
$%5.2f" %priceOfMeal)
print("
-------")
print("Tax (8%%)
$%5.2f" %tax2)
print("
-------")
print("TOTAL
$%5.2f" %total2)
else:
print("Invalid answer, please try again.")
Pygame box
import pygame
pygame.init()
width = int(input("Enter the width of the screen: "))
length = int(input("Enter the length of the screen: "))
xcoordinate = int(input("Enter the x-coordinate of where you want your box to be: "))
ycoordinate = int(input("Enter the y-coordinate of where you want your box to be: "))
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
SIZE = (width, length)
screen = pygame.display.set_mode(SIZE)
def drawbox(x,y):
pygame.draw.line(screen, (WHITE), (x-100, y-100), (x+100, y-100), 5)
pygame.draw.line(screen, (WHITE), (x+100, y-100), (x+100, y+100), 5)
pygame.draw.line(screen, (WHITE), (x+100, y+100), (x-100, y+100), 5)
pygame.draw.line(screen, (WHITE), (x-100, y+100), (x-100, y-100), 5)
pygame.draw.line(screen, (WHITE), (x-100, y-100), (x, y-200), 5)
pygame.draw.line(screen, (WHITE), (x, y-200), (x+200, y-200), 5)
pygame.draw.line(screen, (WHITE), (x+200, y-200), (x+200, y), 5)
pygame.draw.line(screen, (WHITE), (x+200, y), (x+100, y+100), 5)
pygame.draw.line(screen, (WHITE), (x+200, y-200), (x+100, y-100), 5)
drawbox(xcoordinate, ycoordinate)
pygame.display.flip()
pygame.time.wait(3000)
pygame.quit()
Graphic Question Ex.2 and Ex.3
#import pygame
#pygame.init()
#BLACK = (0, 0, 0)
#WHITE = (255, 255, 255)
#RED = (255, 0, 0)
#GREEN = (0, 255, 0)
#BLUE = (0, 0, 255)
#SKYBLUE = (52, 235, 232)
#PURPLE = (180, 51, 255)
#SIZE = (500, 500)
#screen = pygame.display.set_mode(SIZE)
#screen.fill(BLUE)
#pygame.draw.rect(screen, (RED), pygame.Rect(200, 200, 100, 100))
#pygame.display.flip()
#pygame.time.wait(2000)
import pygame
pygame.init()
width = int(input("Enter the width of the screen: "))
length = int(input("Enter the length of the screen: "))
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
SKYBLUE = (52, 235, 232)
PURPLE = (180, 51, 255)
SIZE = (width, length)
screen = pygame.display.set_mode(SIZE)
def drawrectangle(x, y):
pygame.draw.rect(screen, (RED), pygame.Rect((x/2)-((x/5)/2), (y/2)-((y/5)/2), x/5, y/5))
screen.fill(BLUE)
drawrectangle(width, length)
pygame.display.flip()
pygame.time.wait(2000)
Haunted House Assignment
https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwjbkqqr1KnlAhVYj54KHf7
RCB4QjRx6BAgBEAQ&url=https%3A%2F%2Fdepositphotos.com%2F219692182%2Fstock-illu
stration-vector-illustration-cartoon-creepy-haunted.html&psig=AOvVaw3UWVuv1zQ5mGHXeIF
GgF5E&ust=1571620380651236
#Name: Miranda
#Date: Oct. 18, 2019
#Class: ICS3U1-04
#Description: Design a haunted house
import random
import pygame
pygame.init()
SIZE = (800, 600)
screen = pygame.display.set_mode(SIZE)
#Colours
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
ORANGE = (222, 52, 0)
DARKYELLOW = (209, 202, 2)
YELLOW = (255, 247, 5)
PALEYELLOW = (255, 249, 85)
PALEYELLOW2 = (255, 250, 100)
PALERYELLOW = (255, 250, 115)
PALERYELLOW2 = (255, 250, 130)
PALESTYELLOW = (255, 251, 150)
PALESTYELLOW2 = (255, 255, 170)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
NIGHTBLUE = (22, 50, 204)
NIGHTBLUE2 = (10, 38, 191)
DARKNIGHTBLUE = (5, 30, 173)
DARKNIGHTBLUE2 = (2, 25, 158)
DARKERNIGHTBLUE = (11, 0, 161)
DARKERNIGHTBLUE2 = (10, 0, 140)
EVENDARKERNIGHTBLUE = (8, 0, 117)
EVENDARKERNIGHTBLUE2 = (6, 0, 94)
DARKDARKNIGHTBLUE = (4, 0, 64)
DARKDARKNIGHTBLUE2 = (3, 0, 52)
DARKESTNIGHTBLUE = (0, 3, 41)
DARKESTNIGHTBLUE2 = (0, 2, 26)
SKYBLUE = (14, 219, 237)
LIGHTPURPLE = (194, 94, 209)
PURPLE = (149, 0, 255)
SHADOWPURPLE = (125, 0, 225)
DARKPURPLE = (100, 0, 200)
DARKERPURPLE = (49, 0, 102)
DARKESTPURPLE = (20, 0, 56)
BROWN = (74, 18, 1)
#Functions
def background():
screen.fill(NIGHTBLUE)
pygame.draw.rect(screen, (DARKESTPURPLE), pygame.Rect(0, 400, 800, 200))
pygame.draw.polygon(screen, (BLACK), ((250, 525), (220, 585), (580, 585), (550, 525)))
pygame.draw.rect(screen, (DARKNIGHTBLUE), pygame.Rect(0, 360, 800, 40))
pygame.draw.rect(screen, (DARKNIGHTBLUE2), pygame.Rect(0, 320, 800, 40))
pygame.draw.rect(screen, (DARKERNIGHTBLUE), pygame.Rect(0, 280, 800, 40))
pygame.draw.rect(screen, (DARKERNIGHTBLUE2), pygame.Rect(0, 240, 800, 40))
pygame.draw.rect(screen, (EVENDARKERNIGHTBLUE), pygame.Rect(0, 200, 800, 40))
pygame.draw.rect(screen, (EVENDARKERNIGHTBLUE2), pygame.Rect(0, 160, 800, 40))
pygame.draw.rect(screen, (DARKDARKNIGHTBLUE), pygame.Rect(0, 120, 800, 40))
pygame.draw.rect(screen, (DARKDARKNIGHTBLUE2), pygame.Rect(0, 80, 800, 40))
pygame.draw.rect(screen, (DARKESTNIGHTBLUE), pygame.Rect(0, 40, 800, 40))
pygame.draw.rect(screen, (DARKESTNIGHTBLUE2), pygame.Rect(0, 0, 800, 40))
pygame.draw.circle(screen, (PALESTYELLOW2), (400, 250), 200)
pygame.draw.circle(screen, (PALESTYELLOW), (400, 250), 175)
pygame.draw.circle(screen, (PALERYELLOW2), (400, 250), 150)
pygame.draw.circle(screen, (PALERYELLOW), (400, 250), 125)
pygame.draw.circle(screen, (PALEYELLOW2), (400, 250), 100)
pygame.draw.circle(screen, (PALEYELLOW), (400, 250), 75)
def house():
pygame.draw.rect(screen, (PURPLE), pygame.Rect(250, 375, 300, 150)) #1st floor
pygame.draw.rect(screen, (DARKERPURPLE), pygame.Rect(250, 300, 300, 75)) #1st floor
roof -> ->
pygame.draw.polygon(screen, (DARKERPURPLE), ((250, 300), (250, 375), (225, 375)))
pygame.draw.polygon(screen, (DARKERPURPLE), ((550, 300), (550, 375), (575, 375)))
pygame.draw.rect(screen, (SHADOWPURPLE), pygame.Rect(250, 375, 300, 25)) #1st floor
shadow
pygame.draw.rect(screen, (PURPLE), pygame.Rect(275, 225, 250, 125)) #2nd floor
pygame.draw.rect(screen, (PURPLE), pygame.Rect(275, 125, 50, 100)) #Left pillar
pygame.draw.rect(screen, (SHADOWPURPLE), pygame.Rect(315, 124, 10, 100)) #Left pillar
shadow
pygame.draw.rect(screen, (PURPLE), pygame.Rect(450, 75, 75, 150)) #Right pillar
pygame.draw.rect(screen, (SHADOWPURPLE), pygame.Rect(505, 75, 20, 150)) #Right pillar
shadow
pygame.draw.polygon(screen, (DARKERPURPLE), ((265, 125), (300, 75), (335, 125))) #Left
pillar roof
pygame.draw.polygon(screen, (DARKERPURPLE), ((440, 75), (488, 25), (535, 75))) #Right
pillar roof
pygame.draw.circle(screen, (DARKPURPLE), (400, 425), 100) #Semi circle roof
pygame.draw.circle(screen, (DARKERPURPLE), (400, 395), 60) #Semi circle window border
pygame.draw.circle(screen, (PALEYELLOW), (400, 395), 50) #Semi circle window
pygame.draw.rect(screen, (DARKPURPLE), (340, 380, 120, 20))
pygame.draw.rect(screen, (DARKERPURPLE), (342, 380, 115, 8))
pygame.draw.line(screen, (DARKERPURPLE), (400, 340), (400, 385), 5) #Separating circle
window glass -> ->
pygame.draw.line(screen, (DARKERPURPLE), (435, 355), (400, 385), 5)
pygame.draw.line(screen, (DARKERPURPLE), (365, 355), (400, 385), 5)
pygame.draw.rect(screen, (PURPLE), pygame.Rect(275, 400, 250, 125)) #Covers semi circle
roof
pygame.draw.polygon(screen, (DARKERPURPLE), ((225, 225), (400, 125), (575, 225))) #2nd
floor roof
pygame.draw.rect(screen, (SHADOWPURPLE), pygame.Rect(275, 225, 250, 25)) #2nd floor
shadow
pygame.draw.line(screen, (SHADOWPURPLE), (450, 143.571), (524, 186.429), 20) #Right
pillar/2nd floor roof shadow
pygame.draw.line(screen, (SHADOWPURPLE), (275, 186.429), (324, 157.857), 20) #Left
pillar/2nd floor roof shadow
pygame.draw.rect(screen, (DARKERPURPLE), pygame.Rect(340, 400, 20, 125)) #Left door
pillar
pygame.draw.rect(screen, (DARKERPURPLE), pygame.Rect(440, 400, 20, 125)) #Right door
pillar
pygame.draw.rect(screen, (SHADOWPURPLE), pygame.Rect(325, 520, 150, 10)) #1st stair
pygame.draw.rect(screen, (DARKPURPLE), pygame.Rect(300, 530, 200, 10)) #2nd stair
pygame.draw.rect(screen, (DARKERPURPLE), pygame.Rect(275, 540, 250, 10)) #3rd stair
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(375, 420, 50, 100)) #Door
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(375, 420, 50, 10)) #Door shadow
->
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(415, 420, 10, 100))
pygame.draw.rect(screen, (DARKERPURPLE), pygame.Rect(265, 420, 60, 70)) #Left window
border
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(270, 425, 22.5, 27.5)) #Top left left
window glass
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(270, 425, 22.5, 5)) #Top left left
window glass shadow ->
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(287.5, 425, 5, 27.5))
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(297.5, 425, 22.5, 27.5)) #Top right
left window glass
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(297.5, 425, 22.5, 5))#Top right left
window glass shadow ->
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(315, 425, 5, 27.5))
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(270, 457.5, 22.5, 27.5)) #Bottom
left left window glass
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(270, 457.5, 22.5, 5)) #Bottom left
left window glass shadow ->
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(287.5, 457.5, 5, 27.5))
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(297.5, 457.5, 22.5, 27.5)) #Bottom
right left window glass
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(297.5, 457.5, 22.5, 5)) #Bottom
right left window glass shadow ->
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(315, 457.5, 5, 27.5))
pygame.draw.rect(screen, (DARKERPURPLE), pygame.Rect(475, 420, 60, 70)) #Right
window border
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(480, 425, 22.5, 27.5)) #Top left
right window glass
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(480, 425, 22.5, 5)) #Top left right
window glass shadow ->
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(497.5, 425, 5, 27.5))
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(507.5, 425, 22.5, 27.5)) #Top right
right window glass
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(507.5, 425, 22.5, 5))#Top right
right window glass shadow ->
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(525, 425, 5, 27.5))
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(480, 457.5, 22.5, 27.5)) #Bottom
left right window glass
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(480, 457.5, 22.5, 5)) #Bottom left
right window glass shadow ->
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(497.5, 457.5, 5, 27.5))
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(507.5, 457.5, 22.5, 27.5)) #Bottom
right right window glass
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(507.5, 457.5, 22.5, 5)) #Bottom
right right window glass shadow ->
pygame.draw.rect(screen, (DARKYELLOW), pygame.Rect(525, 457.5, 5, 27.5))
pygame.draw.rect(screen, (DARKERPURPLE), pygame.Rect(370, 265, 60, 50)) #2nd floor
middle window border ->
pygame.draw.circle(screen, (DARKERPURPLE), (400, 265), 30)
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(375, 270, 50, 40)) #2nd floor
middle window glass ->
pygame.draw.circle(screen, (PALEYELLOW), (400, 265), 25)
pygame.draw.line(screen, (DARKERPURPLE), (375, 270), (425, 270), 5) #2nd floor middle
window divider ->
pygame.draw.line(screen, (DARKERPURPLE), (400, 240), (400, 270), 5)
pygame.draw.rect(screen, (DARKERPURPLE), pygame.Rect(303, 265, 40, 70)) #2nd floor
left window border
pygame.draw.circle(screen, (DARKERPURPLE), (323, 265), 20)
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(308, 270, 30, 60)) #2nd floor left
window glass ->
pygame.draw.circle(screen, (PALEYELLOW), (323, 265), 15)
pygame.draw.line(screen, (DARKERPURPLE), (308, 270), (338, 270), 5) #2nd floor left
window divider -> -> ->
pygame.draw.line(screen, (DARKERPURPLE), (323, 270), (323, 330), 5)
pygame.draw.line(screen, (DARKERPURPLE), (308, 300), (338, 300), 5)
pygame.draw.rect(screen, (DARKERPURPLE), pygame.Rect(457, 265, 40, 70)) #2nd floor
right window border
pygame.draw.circle(screen, (DARKERPURPLE), (477, 265), 20)
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(462, 270, 30, 60)) #2nd floor right
window glass ->
pygame.draw.circle(screen, (PALEYELLOW), (477, 265), 15)
pygame.draw.line(screen, (DARKERPURPLE), (462, 270), (492, 270), 5) #2nd floor right
window divider -> -> ->
pygame.draw.line(screen, (DARKERPURPLE), (477, 270), (477, 330), 5)
pygame.draw.line(screen, (DARKERPURPLE), (462, 300), (492, 300), 5)
pygame.draw.circle(screen, (DARKPURPLE), (400, 180), 30) #2nd floor roof window border
pygame.draw.circle(screen, (PALEYELLOW), (400, 180), 25) #2nd floor roof window glass
pygame.draw.line(screen, (DARKPURPLE), (400, 155), (400, 205), 5) #2nd floor roof window
divider ->
pygame.draw.line(screen, (DARKPURPLE), (375, 180), (425, 180), 5)
pygame.draw.rect(screen, (DARKERPURPLE), pygame.Rect(467, 100, 40, 50)) #Right pillar
window
pygame.draw.rect(screen, (PALEYELLOW), pygame.Rect(472, 105, 30, 40)) #Right pillar
window glass
pygame.draw.line(screen, (DARKERPURPLE), (472, 125), (502, 125), 5) #Right pillar window
divider ->
pygame.draw.line(screen, (DARKERPURPLE), (487, 105), (487, 145), 5)
def tree():
pygame.draw.polygon(screen, (BLACK), ((100, 400), (100, 360), (80, 340), (80, 320), (60,
300), (90, 320), (90, 340), (100, 350), (100, 300), (60, 260), (60, 240), (40, 220), (70, 240), (70,
260), (90, 280), (90, 240), (100, 220), (60, 180), (60, 140), (70, 160), (70, 180), (100, 210), (120,
180), (120, 140), (140, 120), (130, 140), (130, 180), (110, 220), (140, 200), (160, 180), (150,
200), (100, 240), (100, 280), (120, 240), (140, 220), (130, 240), (110, 280), (110, 300), (120,
280), (160, 240), (180, 240), (160, 250), (130, 280), (120, 320), (140, 300), (160, 300), (140,
310), (120, 330), (120, 400)))
def fence():
pygame.draw.line(screen, (BLACK), (550, 370), (790, 370), 5)
pygame.draw.line(screen, (BLACK), (550, 330), (550, 400), 5)
pygame.draw.line(screen, (BLACK), (570, 340), (570, 400), 5)
pygame.draw.line(screen, (BLACK), (590, 330), (590, 400), 5)
pygame.draw.line(screen, (BLACK), (610, 340), (610, 400), 5)
pygame.draw.line(screen, (BLACK), (630, 330), (630, 400), 5)
pygame.draw.line(screen, (BLACK), (650, 340), (650, 400), 5)
pygame.draw.line(screen, (BLACK), (670, 330), (670, 400), 5)
pygame.draw.line(screen, (BLACK), (690, 340), (690, 400), 5)
pygame.draw.line(screen, (BLACK), (710, 330), (710, 400), 5)
pygame.draw.line(screen, (BLACK), (730, 340), (730, 400), 5)
pygame.draw.line(screen, (BLACK), (750, 330), (750, 400), 5)
pygame.draw.line(screen, (BLACK), (770, 340), (770, 400), 5)
def ghost(x, y):
pygame.draw.circle(screen, (WHITE), (x, y), 20)
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(x-20, y-5, 12, 40))
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(x-10, y-5, 12, 40))
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(x, y-5, 12, 40))
pygame.draw.ellipse(screen, (WHITE), pygame.Rect(x+10, y-5, 12, 40))
pygame.draw.circle(screen, (BLACK), (x-7, y-5), 3)
pygame.draw.circle(screen, (BLACK), (x+7, y-5), 3)
pygame.draw.ellipse(screen, (BLACK), pygame.Rect(x-6, y+5, 12, 15))
def pumpkin(x, y):
pygame.draw.ellipse(screen, (BLACK), pygame.Rect(x-40, y+10, 80, 50))
pygame.draw.rect(screen, (BROWN), pygame.Rect(x-5, y-45, 10, 20))
pygame.draw.ellipse(screen, (ORANGE), pygame.Rect(x-40, y-30, 80, 60))
pygame.draw.circle(screen, (YELLOW), (x, y), 20)
pygame.draw.rect(screen, (ORANGE), pygame.Rect(x-20, y-20, 40, 25))
pygame.draw.polygon(screen, (YELLOW), ((x-20, y-5), (x-15, y-15), (x-10, y-5)))
pygame.draw.polygon(screen, (YELLOW), ((x+20, y-5), (x+15, y-15), (x+10, y-5)))
def housepumpkin(x, y):
pygame.draw.rect(screen, (BROWN), pygame.Rect(x-2.5, y-25, 5, 15))
pygame.draw.ellipse(screen, (ORANGE), pygame.Rect(x-20, y-15, 40, 30))
pygame.draw.circle(screen, (YELLOW), (x, y), 10)
pygame.draw.rect(screen, (ORANGE), pygame.Rect(x-10, y-10, 20, 15))
pygame.draw.polygon(screen, (YELLOW), ((x-10, y-2.5), (x-7.5, y-7.5), (x-5, y-2.5)))
pygame.draw.polygon(screen, (YELLOW), ((x+10, y-2.5), (x+7.5, y-7.5), (x+5, y-2.5)))
def bat():
x = random.randint(100, 700)
y = random.randint(50, 350)
pygame.draw.ellipse(screen, (BLACK), pygame.Rect(x-10, y-15, 20, 30))
pygame.draw.polygon(screen, (BLACK), ((x-50, y-5), (x+50, y-5), (x+40, y+10), (x+33, y+5),
(x+27, y+10), (x+20, y+5), (x+13, y+10), (x, y), (x-13, y+10,), (x-20, y+5), (x-27, y+10), (x-33,
y+5), (x-40, y+10)))
#Outputs the haunted house
background()
fence()
house()
tree()
pumpkin(random.randint(50, 200), random.randint(400, 550))
pumpkin(random.randint(600, 750), random.randint(400, 550))
housepumpkin(random.randint(345, 460), 505)
ghost(random.randint(50, 200), random.randint(50, 350))
ghost(random.randint(50, 200), random.randint(400, 550))
ghost(random.randint(600, 750), random.randint(50, 350))
ghost(random.randint(600, 750), random.randint(400, 550))
bat()
bat()
bat()
pygame.display.flip()
pygame.time.wait(10000)
pygame.quit()
Circles Assignment
#Name: Miranda
#Date: Oct. 24, 2019
#Class: ICS3U1-04
#Description: User inputs 2 circle coordinates and radius and the output tells the user whether
or not they collide
#Level 4+
#User inputs circles' coordinates and radius
firstcircle = input("Please enter the coordinates of the centre and the radius of your first circle in
the format (x,y) radius: ")
secondcircle = input("Please enter the coordinates of the centre and the radius of your second
circle in the format (x,y) radius: ")
#Assigns the coordinates and radius to variables
xfirst = firstcircle[1:firstcircle.find(",")]
yfirst = firstcircle[firstcircle.find(",")+1:firstcircle.find(")")]
firstradius = firstcircle[firstcircle.find(" ")+1:]
xsecond = secondcircle[1:secondcircle.find(",")]
ysecond = secondcircle[secondcircle.find(",")+1:secondcircle.find(")")]
secondradius = secondcircle[secondcircle.find(" ")+1:]
#Checking if all information are vaild numbers
if (xfirst.isdigit() == True or (xfirst[1:].isdigit() == True and xfirst[0] == "-")) and (yfirst.isdigit() ==
True or (yfirst[1:].isdigit() == True and yfirst[0] == "-")) and firstradius.isdigit() == True and
(xsecond.isdigit() == True or (xsecond[1:].isdigit() == True and xsecond[0] == "-")) and
(ysecond.isdigit() == True or (ysecond[1:].isdigit() == True and ysecond[0] == "-")) and
secondradius.isdigit() == True:
#Typecasting the variables to ints for calculations
xfirst = int(xfirst)
yfirst = int(yfirst)
firstradius = int(firstradius)
xsecond = int(xsecond)
ysecond = int(ysecond)
secondradius = int(secondradius)
#Calculating the distance using the formula of the distance between two points
distance = ((xsecond-xfirst)**2+(ysecond-yfirst)**2)**0.5
#Outputs if the circles collide or not
if distance > (firstradius + secondradius):
print("Your circles do not collide.")
elif distance < (firstradius + secondradius):
print("Your circles collide.")
elif distance == (firstradius + secondradius):
print("Your circles are touching at the very edge.")
#Output if information is invalid
else:
print("Information entered is invalid. Please try again. Make sure you have a comma between
your x and y coordinates, a space between your radius and coordinate and no decimals.")
#User inputs circles' coordinates and radius
firstcircle = input("Please enter the coordinates of the centre and the radius of your first circle in
the format (x,y) radius: ")
secondcircle = input("Please enter the coordinates of the centre and the radius of your second
circle in the format (x,y) radius: ")
##Level 4
##Assigns the coordinates and radius to variables
#xfirst = firstcircle[1:firstcircle.find(",")]
#yfirst = firstcircle[firstcircle.find(",")+1:firstcircle.find(")")]
#firstradius = firstcircle[firstcircle.find(" ")+1:]
#xsecond = secondcircle[1:secondcircle.find(",")]
#ysecond = secondcircle[secondcircle.find(",")+1:secondcircle.find(")")]
#secondradius = secondcircle[secondcircle.find(" ")+1:]
##Typecasting the variables to floats for calculations
#xfirst = int(xfirst)
#yfirst = int(yfirst)
#firstradius = int(firstradius)
#xsecond = int(xsecond)
#ysecond = int(ysecond)
#secondradius = int(secondradius)
##Calculating the distance using the formula of the distance between two points
#distance = ((xsecond-xfirst)**2+(ysecond-yfirst)**2)**0.5
##Outputs if the circles collide or not
#if distance > (firstradius + secondradius):
#print("Your circles do not collide.")
#elif distance < (firstradius + secondradius):
#print("Your circles collide.")
#elif distance == (firstradius + secondradius):
#print("Your circles are touching at the very edge.")
moreselectionEx1
card1 = input("Please enter your first card: ")
card2 = input("Please enter your second card: ")
rank1 = card1[:card1.find(" ")]
rank2 = card2[:card2.find(" ")]
suit1 = card1[card1.find(" ")+4:]
suit2 = card2[card2.find(" ")+4:]
if card1.lower() == "seven of spades":
print("Your first card wins.")
elif card2.lower() == "seven of spades":
print("Your second card wins.")
elif suit1.lower() == "hearts" and (suit2.lower() == "diamonds" or suit2.lower() == "spades" or
suit2.lower() == "clubs"):
print("Your first card wins.")
elif suit2.lower() == "hearts" and (suit1.lower() == "diamonds" or suit1.lower() == "spades" or
suit1.lower() == "clubs"):
print("Your second card wins.")
elif suit1.lower() == "diamonds" and (suit2.lower() == "spades" or suit2.lower() == "clubs"):
print("Your first card wins.")
elif suit2.lower() == "diamonds" and (suit1.lower() == "spades" or suit1.lower() == "clubs"):
print("Your second card wins.")
elif suit1.lower() == "spades" and suit2.lower() == "clubs":
print("Your first card wins.")
elif suit2.lower() == "spades" and suit1.lower() == "clubs":
print("Your second card wins.")
elif suit1.lower() == suit2.lower():
if rank1.lower() == "ace":
rank1 = int(1)
elif rank1.lower() == "two":
rank1 = int(2)
elif rank1.lower() == "three":
rank1 = int(3)
elif rank1.lower() == "four":
rank1 = int(4)
elif rank1.lower() == "five":
rank1 = int(5)
elif rank1.lower() == "six":
rank1 = int(6)
elif rank1.lower() == "seven":
rank1 = int(7)
elif rank1.lower() == "eight":
rank1 = int(8)
elif rank1.lower() == "nine":
rank1 = int(9)
elif rank1.lower() == "ten":
rank1 = int(10)
elif rank1.lower() == "jack":
rank1 = int(11)
elif rank1.lower() == "queen":
rank1 = int(12)
elif rank1.lower() == "king":
rank1 = int(13)
if rank2.lower() == "ace":
rank2 = int(1)
elif rank2.lower() == "two":
rank2 = int(2)
elif rank2.lower() == "three":
rank2 = int(3)
elif rank2.lower() == "four":
rank2 = int(4)
elif rank2.lower() == "five":
rank2= int(5)
elif rank2.lower() == "six":
rank2 = int(6)
elif rank2.lower() == "seven":
rank2 = int(7)
elif rank2.lower() == "eight":
rank2 = int(8)
elif rank2.lower() == "nine":
rank2 = int(9)
elif rank2.lower() == "ten":
rank2 = int(10)
elif rank2.lower() == "jack":
rank2 = int(11)
elif rank2.lower() == "queen":
rank2 = int(12)
elif rank2.lower() == "king":
rank2 = int(13)
if rank1 > rank2:
print("Your first card wins.")
elif rank2 > rank1:
print("Your second card wins.")
elif card1.lower() == card2.lower():
print("You have the same card. Please try again.")
else:
print("Invalid input. Please try again.")
moreloopsEx3
num1 = int(input("Please enter your first number: "))
num2 = int(input("Please enter your second number: "))
total = 0
while num1 < num2 + 1:
total += num1
num1 += 1
print(total)
moreloopsEx4
num = input("Please enter your number: ")
total = 0
for x in range(len(num)):
digit = int(num[x])
total += digit
print(total)
moreloopsEx5
num = input("Please enter your number: ")
product = 1
count = 0
for x in range(len(num)):
digit = int(num[x])
if digit % 2 != 0:
product *= digit
count += 1
if count == 0:
product = 0
print(product)
randomnumloopsEx1
import random
headcount = 0
tailcount = 0
for x in range(300):
flip = random.choice(("tails", "heads"))
if flip == "tails":
tailcount += 1
else:
headcount += 1
print("Heads:", headcount, "Tails:", tailcount)
randomnumloopsEx2
import random
count = 0
for x in range(1000):
num = random.randint(1, 6)
if num == 5:
count += 1
print("The die was 5,", count, "times.")
randomnumloopsEx3
import random
count = 0
for x in range(10000):
coin1 = random.choice(("heads", "tails"))
coin2 = random.choice(("heads", "tails"))
if coin1 == coin2:
count +=1
print("The two coins were the same", count, "times.")
randomnumloopsEx4
import random
count = 0
while True:
point = random.randint(1, 6)
point2 = random.randint(1,6)
if point == point2:
break
while point != point2:
point2 = random.randint(1,6)
count += 1
print("It takes", count, "rolls to get the same value.")
randomnumloopsEx5
import random
for x in range(10):
cardnum = random.choice(("Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King"))
cardsuit = random.choice(("of Diamonds", "of Hearts", "of Spades", "of Clubs"))
print("Your card is the", cardnum, cardsuit)
randomnumloopsEx6
import random
num = random.randint(1, 100)
count = 0
while True:
guess = int(input("Please guess the number between 1 and 100: "))
count += 1
if guess == num:
break
else:
if guess < num:
print("Too low")
else:
print("Too high")
print("You guessed the number! It's", str(num)+ ". It took", count, "tries.")
randomnumloopsEx7
import random
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
winningnum = die1 + die2
count = 1
if winningnum == 7 or winningnum == 11:
print("You rolled a", winningnum, "on your first try! You win!")
elif winningnum == 2 or winningnum == 3 or winningnum == 12:
print("You rolled a", winningnum, "on your first try. You lose.")
else:
while True:
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
winningnum2 = die1 + die2
count += 1
if winningnum == winningnum2 or winningnum2 == 7:
break
if winningnum == winningnum2:
print("You rolled your winning number of", winningnum, "after", count, "times. You win!")
elif winningnum2 == 7:
print("You rolled a", winningnum2, "after", count, "times. You lose.")
nestedloopsEx1
for count in range(2):
string = ""
for count2 in range(10):
string += "*"
print(string)
nestedloopsEx1.2
#a)
string = ""
for x in range(3,6):
for y in range(4,7):
string += str(x) + str(y) + " "
print(string)
#b)
string = ""
for x in range(1,4):
for y in range(5,9):
string += str(x) + str(y) + " "
string += "\n"
print(string)
nestedloopsEx2
numofrows = int(input("Please input the number of rows you would like: "))
numofcolumns = int(input("Please input the number of columns you would like: "))
string = ""
for count in range(numofrows):
for count2 in range(numofcolumns):
string += "*"
string += "\n"
print(string)
nestedloopsEx4
import pygame
pygame.init()
SIZE = (800, 800)
screen = pygame.display.set_mode(SIZE)
loopsstringEx1
word = input("Please enter your word: ")
for x in range(len(word)):
print(word[x], len(word))
loopsstringEx2
word = input("Please enter your word: ")
for x in range(len(word)):
print(word[len(word)-1-x], len(word))
loopsstringEx3
word = input("Please enter your word: ")
for x in range(len(word)):
print(word[x], word[len(word)-1-x])
loopsstringEx4
word = input("Please enter your word: ")
for x in range(len(word)):
print(word[:x]+word[x].upper()+word[x+1:])
loopsstringEx5
count = 0
string1 = input("Please input your first string: ")
string2 = input("Please input your second string: ")
for x in range(len(string1)):
letter1 = string1[x]
letter2 = string2[x]
if letter1 != letter2:
count += 1
break
if count == 0:
print("They are the same string.")
else:
print("They are not the same string.")
loopsstringEx6
word = input("Please enter your word: ")
for x in range(len(word)):
print(word)
word = word[1:] + word[0]
stringusesEx1
##a)
#entry = input("Please enter letters: ")
#if entry.isalpha() == True:
#print("Thank you for the correct entry.")
#else:
#print("I SAID LETTERS!!!")
#b)
entry = input("Please enter something that begins with a digit, the rest are letters, and it must
have at least 8 letters: ")
if entry[0].isdigit() and entry[1:].isalpha() and len(entry[1:]) >= 8:
print("Thank you for your valid input.")
else:
print("Please enter a valid input!!")
stringusesEx2
def encrypt(message, shift):
encrypt = ""
for let in message:
value = ord(let) # changes character to ascii number
newValue = value + int(shift) # increase the ascii value by 3
if (value >= 97 and newValue > 122) or (value <= 90 and newValue > 90):
newValue -= 26
newLet = chr(newValue) # change the value back to a character
encrypt += newLet # add it back to the string
return encrypt
sentence = input("Please enter a message you would like encrypted: ")
moves = input("Please enter the number of letters you would like to be shifted: ")
encryption = encrypt(sentence,moves)
string = ""
counter = 1
for x in range(len(encryption)):
string += encryption[x]
if counter % 4 == 0:
string += " "
counter += 1
print(string)
stringusesEx3
#import random
##a)
#sentence = input("Please input your sentence: ")
#vowels = "aeiou"
#for x in vowels:
#if x in sentence:
#sentence = sentence.replace(x,"**")
#print(sentence)
##b)
#sentence = input("Please enter your sentence: ")
#keyword = input("Please enter your keyword: ")
#sentence = sentence.replace(keyword,"**")
#print(sentence)
##c)
#sentence = input("Please enter your sentence: ") + " "
#string = ""
#sentence2 = sentence
#for i in range(sentence.count(" ")+1):
#word = sentence2[:sentence2.find(" ")]
#sentence2 = sentence2[sentence2.find(" ")+1:]
#if len(word) == 4:
#sentence = sentence.replace(word,"****")
#print(sentence)
##d)
#sentence = input("Please enter your sentence: ")
#string = ""
#for x in range(sentence.count(" ")):
#word = sentence[:sentence.find(" ")]
#sentence = sentence[sentence.find(" ")+1:]
#string = word + " " + string
#string = sentence + " " + string
#print(string)
*For math class:
Math1
print("Hello World!")
Math2
#Mini Program 2: Area and Perimeter
#Miranda
#Nov. 25, 2019
#Declare variables
l = 10 #Declare and initiate the length, note this is a letter L not a one
w = 12 #Declare and initiate the width
#Calculations
area = l*w
perimeter = 2*(l+w)
#Output
print("The area of a rectangle with a length of", str(l), "and a width of", str(w), "is", str(area) + ".")
print("The perimeter of a rectangle with a length of", str(l), "and a width of", str(w), "is",
str(perimeter) + ".")
Math3
#Mini Program 3: Area of a Triangle
#Miranda
#Nov. 25, 2019
#Declare variables
b = 7 #Declare and initiate the base
h = 6 #Declare and initiate the height
#Calculations
area = b*h/2
#Output
print("The area of a triangle with a base of", str(b), "and a height of", str(h), "is", str(area) + ".")
Math4
#Mini Program 4: Area and Circumference of a Circle
#Miranda
#Nov. 25, 2019
import math
#Declare variables
radius = 5 #Declare and initiate the radius
#Calculations
area = math.pi*radius**2
circumference = 2*math.pi*radius
#Output
print("The area is", str(round(area,2)) + ".")
print("The circumference is", str(round(circumference,2)) + ".")
#Mini-Challenge
#Declare variables
diameter = 10 #Declare and initiate the diameter
#Calculations
radius = diameter/2
area = math.pi*radius**2
circumference = 2*math.pi*radius
#Output
print("The area is", str(round(area,2)) + ".")
print("The circumference is", str(round(circumference,2)) + ".")
Math5
#Mini Program 5: Pythagorean Theorem
#Miranda
#Nov. 25, 2019
import math
#Declare variables
side1 = 12 #Declare and initiate the first side
side2 = 5 #Declare and initiate the second side
#Calculations
hypotenuse = math.sqrt(side1**2 + side2**2)
#Output
print("The length of the hypotenuse is", str(hypotenuse) + ".")
#Mini-Challenge
#Declare variables
side1 = 12 #Declare and initiate the first side
hypotenuse = 13 #Declare and initiate the hypotenuse
#Calculations
side2 = math.sqrt(hypotenuse**2-side1**2)
#Output
print("The length of the second side is", str(side2) + ".")
Math6
#Mini Program 6: Discriminant
#Miranda
#Nov. 25, 2019
#Declare variables
a = 5 #Declare and initiate a
b = 10 #Declare and initiate b
c = 5 #Declare and initiate c
#Calculations
discriminant = b**2 - 4*a*c
#Finding the number of roots
if discriminant > 0:
numofroots = "There are two distinct roots."
elif discriminant == 0:
numofroots = "There is one distinct root."
else:
numofroots = "There are zero distinct roots."
#Output
print("The discriminant is", str(discriminant) + ".")
print(numofroots)
Math7
#Mini Program 7: Quadratic Formula
#Miranda
#Nov. 25, 2019
import math
#Declare variables
a = -3 #Declare and initiate a
b = 6 #Declare and initiate b
c = 3 #Declare and initiate c
#Calculations
discriminant = b**2 - 4*a*c
#Finding the number of roots
if discriminant > 0:
numofroots = "There are two distinct roots"
root1 = round((-b+math.sqrt(discriminant))/(2*a),2)
root2 = round((-b-math.sqrt(discriminant))/(2*a),2)
#Output
print("The discriminant is", str(discriminant) + ".")
print(numofroots + ",", root1, "and", str(root2) + ".")
elif discriminant == 0:
numofroots = "There is one distinct root"
root = round(-b/(2*a),2)
#Output
print("The discriminant is", str(discriminant) + ".")
print(numofroots + ",", str(root) + ".")
else:
print("The discriminant is", str(discriminant) + ".")
numofroots = "There are zero distinct roots."
listEx3
NUMOFKIDS = 3
listofnames = []
listofweeds = []
totalweeds = 0
for x in range(NUMOFKIDS):
name = input("Please enter the name of the kid: ")
numofweeds = int(input("Please enter the number of weeds they picked: "))
totalweeds += numofweeds
listofnames += [name]
listofweeds += [numofweeds]
for y in range(NUMOFKIDS):
percentage = listofweeds[y]/totalweeds*100
print("%-15 $%5.2f" %(listofnames[y], percentage))
listEx4
import random
dice = [1,2,3,4,5,6]
ladderbase = [6,24,30,49,82]
laddertop = [17,26,44,62,86]
snakeend = [14,20,39,88,66,69,79,84]
snakehead = [3,15,33,36,53,58,67,71]
spot1 = 0
spot2 = 0
winner = 0
while True:
input("\nPlayer 1 hit enter to roll.")
roll = random.choice(dice)
print("You rolled:",roll)
spot1 += roll
if spot1 in ladderbase:
place = ladderbase.index(spot1)
spot1 = laddertop[place]
print("You climbed a ladder.")
elif spot1 in snakeend:
place = snakeend.index(spot1)
spot1 = snakehead[place]
print("You slid down a snake.")
if spot1 > 90:
spot1 -= roll
print("You went past 90.")
elif spot1 == 90:
winner += 1
break
print("You are at spot", str(spot1) + ".")
input("\nPlayer 2 hit enter to roll.")
roll = random.choice(dice)
print("You rolled:",roll)
spot2 + = roll
if spot2 in ladderbase:
place = ladderbase.index(spot2)
spot2 = laddertop[place]
print("You climbed a ladder.")
elif spot2 in snakeend:
place = snakeend.index(spot2)
spot2 = snakehead[place]
print("You slid down a snake.")
if spot2 > 90:
spot2 -= roll
print("You went past 90.")
elif spot2 == 90:
winner += 2
break
print("You are at spot", str(spot2) + ".")
if winner == 1:
print("Player 1 reached 90! You win!")
elif winner == 2:
print("Player 2 reached 90! You win!")
morelistEx1
numbers = []
for x in range(10):
num = int(input("Please input your number: "))
numbers += [num]
numbers.sort(reverse = True)
median = (numbers[len(numbers)//2] + numbers[len(numbers)//2-1])/2
print(numbers)
print(median)
morelistsEx2
names = []
for x in range(5):
name = input("Please enter the name: ")
names += [name]
names.sort()
print(names)
morelistsEx3
list1 = []
list2 = []
newlist = []
while True:
value1 = input("Please enter your value for your first list (or end): ")
if value1 == "end":
break
value1 = int(value1)
list1 += [value1]
while True:
value2 = input("Please enter your value for your second list (or end): ")
if value2 == "end":
break
value2 = int(value2)
list2 += [value2]
newlist = list1 + list2
newlist.sort()
print(newlist)
morelistsEx4
import random
suits = ["Clubs", "Diamonds", "Hearts", "Spades"]
values = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack",
"Queen", "King"]
deck = []
hand1 = []
hand2 = []
for s in suits:
for v in values:
deck.append(v + " of " + s)
random.shuffle(deck)
for x in range(5):
hand1 += [deck.pop()]
hand2 += [deck.pop()]
print(hand1)
print(hand2)
filesEx2
# Reading from a file
numFile = open("ages.txt", "r")
total = 0
counter = 0
while True:
text = numFile.readline()
#rstrip removes the newline character read at the end of the line
if text=="":
break
text = text.rstrip("\n")
counter += 1
total += int(text[text.find(" "):]) # breaks up data into elements of the list values
numFile.close()
average = total/counter
print("The average age is", str(average) + ".")
Download