3.15 Challenge #1

Write a function that will a simulate a coinflip and print the output

import random
def coinflip():
    x = random.randint(0,1)
    if x == 1:
        print("heads")
    else:
        print("tails")
for i in range(20):
    coinflip()
tails
heads
heads
heads
tails
tails
heads
tails
tails
tails
tails
tails
heads
tails
tails
tails
tails
tails
heads
tails

EXTRA: Create a function that will randomly select 5 playing Cards and check if the 5 cards are a Royal Flush

Homework

Given a random decimal number convert it into binary as Extra convert it to hexidecimal as well.

from random import randint
decimal = randint(0,10000)
def convertToBinary(n):
    thelist = []
    while n > 0:
        if n % 2 == 0:
            thelist.append(0)
            n = n / 2
        else:
            thelist.append(1)
            n = n / 2
            n = n - 0.5
    x = len(thelist) - 1
    final = str(thelist[x])
    x = x - 1
    while x >= 0:
        final = final + str(thelist[x])
        x = x - 1
    print(final)

convertToBinary(decimal)
101100110111

3.14 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.
from time import time as clockyboi
start = clockyboi()
end = clockyboi()
print(end-start)
6.222724914550781e-05

Challenge 2: Turtle

Turtle is a python package 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()

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) __
gcd(x,y) 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!
    Documentation
from math import *

number1 = input('')
number2 = input('')
print(ceil(float(number1)))
print(floor(float(number1)))
print(ceil(float(number2)))
print(floor(float(number2)))
print(lcm(int(number1),int(number2)))
print(gcd(int(number1),int(number2)))
print(factorial(int(number1)))
print(factorial(int(number2)))
print(remainder(int(number1),int(number2)))
15
15
30
30
30
15
1307674368000
265252859812191058636308480000000
15.0

Homework: Putting it all together(complete only after the random values lesson)

Option 1: Create a python program which generates a random number between 1 and 10, and use turtle to draw a regular polygon with that many sides. As a hint, remember that the total sum of all the angles in a polygon is (the number of sides - 2) * 180. Note: a regular polygon has all sides and angles the same size. Paste a screenshot of the code and the drawing from repl.it

Option 2: use the "datetime" package, and looking up documentation, create a program to generate 2 random dates and find the number of days between

Extra ideas: customize the settings, draw a picture, or something else!

from PIL import Image

#read the image
im = Image.open("../images/copinghard.png")

#show image
im.show()