Unit 3 Sections 14-15 Homework and Challenges
This is the homework for Unit 3 Sections 14-15
- Notes
- 3.14 Libraries Challenges and Homework
- 3.15 - Random Values Challenges and Homework
- Homework - Random Values
Notes
3.14
(Fill in the blank)
Okay, so we've learned a lot of code, and all of you now can boast that you can code at least some basic programs in python. But, what about more advanced stuff? What if there's a more advanced program you don't know how to make? Do you need to make it yourself? Well, not always.
You've already learned about functions that you can write to reuse in your code in previous lessons. But,there are many others who code in python just like you. So why would you do again what someone has already done, and is available for any python user?
Packages allow a python user to import methods from a library, and use the methods in their code. Most package come with documentation on the different methods they entail and how to use them, and they can be found with a quick google search. methods are used with the following:
Note: a method from a package can only be used after the import statement. Some methods are always installed, such as those with the list methods which we have previously discussed. But others require a special python keyword called import. We will learn different ways to import in Challenge 1.
Sometimes we only need to import a single method from the package. We can do that with the word "from", followed by the package name, then the word "import", then the method. This will alllow you to use the method without mentioning the package's name, unlike what we did before, however other methods from that package cannot be used. To get the best of both worlds you can use "*".
To import a method as an easier name, just do what we did first, add the word "as", and write the name you would like to use that package as.
3.15
Random Values are a number generated using a large set of numbers and a mathematical algorithm which gives equal probability to all number occuring
Each Result from randomization is equally likely to occur Using random number generation in a program means each execution may produce a different result
Examples:
- Dice
- Marbles
Used in coding:
- Games
- Statistical Sampling
- Cryptography
import math
x = math.factorial(8)
print(x)
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.
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
import math
x = int(input("Choose a number"))
y = int(input("Choose a second number"))
print(math.ceil(x))
print(math.ceil(y))
print(math.floor(x))
print(math.floor(y))
print(math.lcm(x,y))
print(math.gcd(x,y))
print(math.factorial(x))
print(math.factorial(y))
print(math.nextafter(x,y))
Homework - Libraries
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!
import random
def coinflip():
x = ""
for i in range(4):
flip = random.randint(1,2)
if flip == 1:
x = "Heads"
elif flip == 2:
x = "Tails"
print("Flip #", i+1, ":", x)
coinflip()
import random
x = random.randint(1, 50)
c = bin(x)
y = hex(int(c.replace("0b","0")))
print("The binary of", x, "is:", int(c.replace("0b", "0")))
print("The hexadecimal of", x, "is:", y)