Homework 3.9, 3.11
- Challenge 1: Basic Libraries
- Challenge 2: Turtle
- Random Challenge
- Library Homework
- Random Homework
- Homework
import math
from math import sqrt
import math as m
Challenge 2: Turtle
Turtle is a python library which allows you to draw all kinds of different shapes. It's ofter used to teach beginning python learners, but is really cool to use anywhere. Turtle employs a __ to display what you've done, but unfortunately it's kind of annoying to make work with vscode.
Use: repl.it
Click "+ Create", and for language, select "Python (with Turtle)"
Documentation
Task: Have fun with turtle! Create something that uses at least 2 lines of different lengths and 2 turns with different angles, and changes at least one setting about either the pen or canvas. Also use one command that isn't mentioned on the table below(there are a lot). Paste a screenshot of the code and the drawing from repl.it
Commands
forward(pixels)
right(degrees)
left(degrees)
setpos(x,y)
speed(speed)
pensize(size)
pencolor(color)
Note: Color should be within quotes, like "brown", or "red"
from turtle import * oogway = Turtle()
from turtle import *
oogway = Turtle()
oogway.speed(0)
for i in range(100):
for c in ['red', 'green', 'blue', 'purple']:
oogway.color(c)
oogway.forward(10+20*i)
oogway.right(90)
oogway.penup()
oogway.left(90)
oogway.forward(15)
oogway.left(90)
oogway.forward(5)
oogway.right(180)
oogway.pendown()
Challenge 3: Math The math package allows for some really cool mathematical methods!
methods Action ceil(x) rounds to largest intefer less than or equal to x factorial(x) returns the greatest common denominator of x and y lcm(x,y) __ Challenge: Create a program which asks for a user input of two numbers, and returns the following:
- each number rounded up
- each number rounded down
- the lcm of the rounded down numbers
- the gcf of the rounded up numbers
- the factorial of each number
- something else using the math package!
from math import *
def mathStuff(a,b):
ans=[]
ans.append(ceil(a))
ans.append(ceil(b))
ans.append(ceil(a))
ans.append(ceil(b))
c=max(a,b)
d=min(a,b)
s=c
while s%d!=0:
s+=c
ans.append(s)
ans.append(gcd(a,b))
ans.append(factorial(a))
ans.append(factorial(b))
# returns the exponent of one number to the other
ans.append(int(pow(a,b)))
ans.append(int(pow(b,a)))
return ans
a,b=int(input()),int(input())
print(mathStuff(a,b))
import random
def coinflip():
coin=["head","tail"][random.randint(0,1)]
return coin
for i in range(10):
print(coinflip())
import random
SUIT_NAMES = {0 : 'Hearts', 1 : 'Diamonds', 2 : 'Clubs', 3 : 'Spades'}
NUMBER_NAMES = {1 : 'Ace', 2 : 'Two', 3 : 'Three', 4 : 'Four',
5 : 'Five', 6 : 'Six', 7 : 'Seven', 8 : 'Eight',
9 : 'Nine', 10 : 'Ten', 11 : 'Jack', 12 : 'Queen',
13 : 'King'}
class PlayingCard:
def __init__(self, suit, number):
self.suit=suit
self.number=number
def __str__(self):
return NUMBER_NAMES[self.number]+" of "+SUIT_NAMES[self.suit]
class Deck:
def __init__(self):
self.cards = []
for i in range(4):
for j in range(1,14):
self.cards.append(PlayingCard(i,j))
def draw(self, num=0):
return self.cards[num]
def __str__(self):
cardString=''
for card in self.cards:
cardString+=str(card)+' '
return cardString
def shuffle(self):
random.shuffle(self.cards)
def checkFlush():
d=Deck()
d.shuffle()
cards=[]
for i in range(5):
cards.append(d.draw(i))
for c in cards:
print(c,end=", ")
print("")
s=cards[0].suit
flush=True
for c in cards:
if c.suit!=s:
flush=False
if [1,11,12,13].count(c.number)!=0:
flush=False
if flush:
print("This is a Royal Flush")
else:
print("This is not a Royal Flush")
r=0
for i in range(100):
if checkFlush():
r+=1
print("There were",r,"Royal Flushes")
import turtle
import random
from math import *
t = turtle.Turtle()
t.speed(0)
for i in range(10):
r = random.randint(20,100)
c = ['red', 'green', 'blue', 'purple'][random.randint(0,3)]
s = random.randint(3,10)
a = 360/s
f = 2*r*cos(a/2)
t.penup()
t.forward(r)
t.left(90)
t.pendown()
t.color(c)
for i in range(s):
t.left(a)
t.forward(f)
t.penup()
t.left(90+a)
t.forward(r)
def ran2bin():
number=random.randint(1,100)
ans = ""
if ( number == 0 ):
return 0
while ( number ):
ans += str(number&1)
number = number >> 1
ans = ans[::-1]
return ans
for i in range(10):
print(ran2bin())
from datetime import date
import random
days_dictionary = {
1: 31,
2: 28,
3: 31,
4: 30,
5: 31,
6: 30,
7: 31,
8: 31,
9: 30,
10: 31,
11: 30,
12: 31,
}
y,m,d=int(input("Please input a year")),int(input("Please input a month")),int(input("Please input a day"))
userDate=date(y,m,d)
randomDate=date(y,random.randint(1,12),random.randint(1,days_dictionary[m]))
print("User entered date is",userDate)
print("Randomized Date is",randomDate)
timeBetween=0
if userDate>randomDate:
timeBetween=userDate-randomDate
else:
timeBetween=randomDate-userDate
print("The number of days between is", timeBetween.days)
# expected output shown below (or something similar)