Challenge 1: Basic Libraries

  1. Find a python package on the internet and import it
  2. Choose a method from the package and import only the method
  3. import the package as a more convenient name.
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))
[4, 6, 4, 6, 12, 2, 24, 720, 4096, 1296]

Random Challenge

import random
def coinflip():
    coin=["head","tail"][random.randint(0,1)]
    return coin

for i in range(10):
    print(coinflip())
head
tail
head
head
tail
tail
head
tail
head
tail
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")
Seven of Spades, King of Spades, Five of Hearts, Queen of Clubs, Four of Hearts, 
This is not a Royal Flush
King of Spades, Ace of Hearts, Ace of Clubs, Ten of Diamonds, Seven of Spades, 
This is not a Royal Flush
Ten of Spades, Four of Spades, Nine of Spades, Queen of Hearts, Jack of Clubs, 
This is not a Royal Flush
Ten of Clubs, Two of Hearts, Six of Clubs, Four of Diamonds, King of Spades, 
This is not a Royal Flush
Jack of Hearts, Four of Hearts, Queen of Hearts, Nine of Diamonds, Four of Clubs, 
This is not a Royal Flush
Four of Clubs, Nine of Clubs, Two of Diamonds, King of Hearts, Two of Hearts, 
This is not a Royal Flush
Eight of Diamonds, Ten of Diamonds, Three of Diamonds, Ace of Diamonds, Nine of Hearts, 
This is not a Royal Flush
Queen of Diamonds, Queen of Spades, Jack of Clubs, Two of Diamonds, Nine of Clubs, 
This is not a Royal Flush
Queen of Hearts, King of Diamonds, Eight of Spades, Nine of Diamonds, Queen of Clubs, 
This is not a Royal Flush
Six of Diamonds, Nine of Diamonds, Eight of Clubs, King of Hearts, Jack of Hearts, 
This is not a Royal Flush
Six of Hearts, Three of Diamonds, Seven of Diamonds, Seven of Hearts, Queen of Diamonds, 
This is not a Royal Flush
Queen of Clubs, King of Spades, Five of Clubs, Five of Hearts, Three of Diamonds, 
This is not a Royal Flush
Five of Hearts, Ten of Hearts, Ten of Clubs, Two of Hearts, Six of Diamonds, 
This is not a Royal Flush
Five of Diamonds, Ace of Hearts, Two of Hearts, Nine of Spades, Two of Clubs, 
This is not a Royal Flush
Ace of Clubs, Four of Diamonds, Five of Spades, Ten of Spades, Ten of Hearts, 
This is not a Royal Flush
Nine of Clubs, Six of Spades, Queen of Spades, Five of Diamonds, King of Hearts, 
This is not a Royal Flush
Seven of Spades, Eight of Clubs, Six of Hearts, Four of Clubs, Queen of Clubs, 
This is not a Royal Flush
Eight of Clubs, Nine of Diamonds, Five of Spades, Four of Diamonds, Queen of Spades, 
This is not a Royal Flush
Five of Diamonds, Seven of Spades, Ten of Spades, King of Hearts, Two of Diamonds, 
This is not a Royal Flush
Eight of Diamonds, Ace of Diamonds, Six of Diamonds, Two of Hearts, Ace of Spades, 
This is not a Royal Flush
Ace of Diamonds, Two of Diamonds, Three of Spades, Ten of Clubs, Two of Spades, 
This is not a Royal Flush
Jack of Spades, Seven of Spades, Queen of Diamonds, Seven of Diamonds, Ace of Clubs, 
This is not a Royal Flush
Two of Diamonds, Five of Clubs, Jack of Hearts, Queen of Diamonds, Jack of Clubs, 
This is not a Royal Flush
Queen of Spades, Two of Hearts, Five of Spades, Eight of Clubs, Seven of Diamonds, 
This is not a Royal Flush
Ten of Hearts, Nine of Hearts, Nine of Spades, Queen of Diamonds, Eight of Hearts, 
This is not a Royal Flush
Two of Clubs, Seven of Clubs, Three of Diamonds, Jack of Hearts, Queen of Diamonds, 
This is not a Royal Flush
Eight of Clubs, Two of Spades, Nine of Diamonds, Jack of Hearts, Queen of Hearts, 
This is not a Royal Flush
Queen of Hearts, Six of Diamonds, Nine of Clubs, Two of Hearts, Four of Diamonds, 
This is not a Royal Flush
Ten of Hearts, Three of Hearts, Ace of Spades, Six of Hearts, Five of Diamonds, 
This is not a Royal Flush
King of Spades, Eight of Diamonds, Queen of Clubs, Seven of Spades, Nine of Hearts, 
This is not a Royal Flush
Ten of Hearts, Ace of Diamonds, Six of Spades, Three of Hearts, Eight of Hearts, 
This is not a Royal Flush
Ace of Diamonds, Three of Spades, Nine of Hearts, Jack of Spades, Six of Hearts, 
This is not a Royal Flush
Nine of Hearts, Four of Diamonds, King of Clubs, Nine of Spades, Queen of Clubs, 
This is not a Royal Flush
Ace of Clubs, Three of Diamonds, Nine of Diamonds, King of Clubs, Ace of Diamonds, 
This is not a Royal Flush
Queen of Hearts, Two of Hearts, Jack of Hearts, Queen of Clubs, Jack of Clubs, 
This is not a Royal Flush
Ace of Diamonds, Two of Clubs, Queen of Spades, Two of Hearts, Jack of Clubs, 
This is not a Royal Flush
Ten of Clubs, Four of Spades, Six of Hearts, Eight of Diamonds, Queen of Clubs, 
This is not a Royal Flush
Queen of Hearts, Two of Spades, Four of Hearts, Three of Diamonds, Seven of Clubs, 
This is not a Royal Flush
Nine of Clubs, Three of Spades, Ace of Hearts, Three of Hearts, Two of Spades, 
This is not a Royal Flush
Jack of Clubs, Four of Spades, Eight of Spades, Ace of Clubs, Queen of Diamonds, 
This is not a Royal Flush
Queen of Diamonds, Three of Diamonds, King of Spades, Four of Hearts, Jack of Diamonds, 
This is not a Royal Flush
Seven of Hearts, Ace of Clubs, King of Clubs, Queen of Spades, Seven of Diamonds, 
This is not a Royal Flush
Ace of Diamonds, Ten of Spades, Eight of Diamonds, Three of Hearts, Three of Diamonds, 
This is not a Royal Flush
Two of Hearts, Seven of Hearts, Queen of Spades, Eight of Spades, Five of Diamonds, 
This is not a Royal Flush
Jack of Spades, Four of Diamonds, Five of Spades, Four of Spades, Five of Diamonds, 
This is not a Royal Flush
Nine of Hearts, King of Spades, Seven of Clubs, Eight of Clubs, Nine of Clubs, 
This is not a Royal Flush
Five of Spades, Four of Diamonds, Eight of Diamonds, Six of Diamonds, Ten of Hearts, 
This is not a Royal Flush
King of Clubs, Queen of Hearts, Seven of Hearts, Five of Hearts, Queen of Diamonds, 
This is not a Royal Flush
Six of Spades, Two of Diamonds, Queen of Diamonds, Six of Clubs, Four of Diamonds, 
This is not a Royal Flush
Six of Spades, Ten of Spades, Two of Diamonds, Seven of Hearts, Six of Hearts, 
This is not a Royal Flush
Seven of Hearts, Three of Diamonds, Ten of Spades, Eight of Diamonds, Jack of Spades, 
This is not a Royal Flush
Six of Clubs, Three of Diamonds, Nine of Diamonds, King of Spades, Jack of Hearts, 
This is not a Royal Flush
Eight of Diamonds, Three of Hearts, Five of Spades, Jack of Clubs, Jack of Diamonds, 
This is not a Royal Flush
Seven of Diamonds, Ace of Spades, Queen of Clubs, Queen of Hearts, Five of Spades, 
This is not a Royal Flush
Queen of Hearts, Seven of Clubs, Two of Spades, King of Diamonds, Ace of Hearts, 
This is not a Royal Flush
Five of Clubs, Four of Spades, Two of Diamonds, Nine of Clubs, Eight of Spades, 
This is not a Royal Flush
Ace of Hearts, Three of Spades, Six of Clubs, Eight of Diamonds, Two of Hearts, 
This is not a Royal Flush
Ten of Clubs, Six of Clubs, Jack of Clubs, King of Hearts, Three of Spades, 
This is not a Royal Flush
Two of Clubs, Jack of Diamonds, Six of Clubs, Jack of Spades, Jack of Clubs, 
This is not a Royal Flush
Queen of Clubs, Two of Diamonds, Nine of Spades, Three of Clubs, Ace of Clubs, 
This is not a Royal Flush
Six of Spades, Five of Diamonds, Ten of Diamonds, Ten of Clubs, Nine of Hearts, 
This is not a Royal Flush
Two of Spades, Ten of Hearts, Six of Hearts, Nine of Hearts, Four of Clubs, 
This is not a Royal Flush
Eight of Clubs, Three of Spades, Eight of Diamonds, Two of Spades, Five of Diamonds, 
This is not a Royal Flush
Two of Spades, Seven of Diamonds, Five of Diamonds, Nine of Clubs, Ace of Diamonds, 
This is not a Royal Flush
King of Clubs, Five of Clubs, Four of Hearts, Five of Hearts, Nine of Hearts, 
This is not a Royal Flush
Ten of Clubs, Nine of Spades, Four of Diamonds, Nine of Diamonds, Jack of Hearts, 
This is not a Royal Flush
Seven of Hearts, Four of Spades, Nine of Clubs, Six of Clubs, Eight of Diamonds, 
This is not a Royal Flush
Five of Diamonds, Ace of Clubs, Queen of Diamonds, Queen of Clubs, Nine of Diamonds, 
This is not a Royal Flush
Queen of Hearts, Nine of Clubs, Queen of Spades, Six of Diamonds, Six of Spades, 
This is not a Royal Flush
Six of Diamonds, Ace of Spades, Jack of Diamonds, Three of Diamonds, Three of Clubs, 
This is not a Royal Flush
Ten of Clubs, Ace of Diamonds, King of Hearts, Six of Diamonds, Eight of Clubs, 
This is not a Royal Flush
Queen of Spades, Eight of Spades, Jack of Spades, Two of Diamonds, Nine of Spades, 
This is not a Royal Flush
Four of Hearts, Three of Diamonds, Eight of Clubs, Eight of Diamonds, Four of Clubs, 
This is not a Royal Flush
Nine of Diamonds, King of Clubs, Five of Diamonds, King of Spades, Two of Spades, 
This is not a Royal Flush
Five of Spades, Ten of Spades, King of Clubs, King of Diamonds, Seven of Spades, 
This is not a Royal Flush
Two of Hearts, Four of Hearts, Seven of Hearts, Jack of Clubs, Ten of Hearts, 
This is not a Royal Flush
Seven of Clubs, King of Hearts, Five of Diamonds, Nine of Hearts, Seven of Hearts, 
This is not a Royal Flush
King of Spades, Eight of Diamonds, Two of Spades, Five of Diamonds, Seven of Spades, 
This is not a Royal Flush
Two of Spades, Five of Diamonds, Three of Diamonds, Eight of Spades, Ace of Diamonds, 
This is not a Royal Flush
Two of Spades, Three of Diamonds, Eight of Hearts, Nine of Hearts, Five of Hearts, 
This is not a Royal Flush
Nine of Spades, Three of Spades, Six of Diamonds, Three of Hearts, Six of Spades, 
This is not a Royal Flush
Eight of Spades, Queen of Hearts, Four of Clubs, Five of Spades, Four of Hearts, 
This is not a Royal Flush
Ace of Spades, Six of Spades, Seven of Clubs, King of Clubs, Four of Clubs, 
This is not a Royal Flush
Five of Diamonds, Seven of Hearts, Ten of Spades, Queen of Hearts, Two of Clubs, 
This is not a Royal Flush
Ten of Spades, Ten of Hearts, Five of Diamonds, Three of Hearts, Nine of Hearts, 
This is not a Royal Flush
Eight of Clubs, Seven of Hearts, Six of Spades, Jack of Diamonds, Nine of Clubs, 
This is not a Royal Flush
Seven of Hearts, Jack of Hearts, Six of Spades, King of Hearts, Ace of Diamonds, 
This is not a Royal Flush
Ace of Spades, Two of Spades, Three of Spades, King of Spades, Eight of Spades, 
This is not a Royal Flush
Nine of Clubs, Ace of Diamonds, Queen of Spades, Nine of Diamonds, Two of Hearts, 
This is not a Royal Flush
Ace of Spades, King of Hearts, King of Diamonds, Ace of Clubs, Seven of Clubs, 
This is not a Royal Flush
Queen of Hearts, Four of Spades, Seven of Diamonds, Three of Diamonds, Queen of Clubs, 
This is not a Royal Flush
Eight of Spades, King of Clubs, Eight of Diamonds, Queen of Clubs, Five of Clubs, 
This is not a Royal Flush
Three of Spades, Two of Spades, King of Diamonds, Two of Clubs, Jack of Diamonds, 
This is not a Royal Flush
Queen of Hearts, Nine of Diamonds, Two of Hearts, Seven of Spades, Eight of Spades, 
This is not a Royal Flush
Three of Hearts, Six of Clubs, Six of Hearts, Jack of Clubs, Eight of Diamonds, 
This is not a Royal Flush
Jack of Hearts, Jack of Spades, Three of Diamonds, Three of Hearts, Eight of Diamonds, 
This is not a Royal Flush
Nine of Hearts, Five of Diamonds, Four of Hearts, Eight of Clubs, King of Spades, 
This is not a Royal Flush
Four of Hearts, Jack of Spades, Seven of Spades, Six of Clubs, Nine of Clubs, 
This is not a Royal Flush
Two of Diamonds, Jack of Clubs, Nine of Clubs, Queen of Clubs, Nine of Hearts, 
This is not a Royal Flush
Ace of Hearts, Nine of Diamonds, Ace of Clubs, Eight of Clubs, Eight of Diamonds, 
This is not a Royal Flush
There were 0 Royal Flushes

Library Homework

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)

Random Homework

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())
101110
10
1011
1110
100110
1010001
1000100
110111
110000
101111

Homework

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)
User entered date is 2010-07-13
Randomized Date is 2010-06-23
The number of days between is 20