3.14 and 3.15 Hacks
import math
A = int(input())
def root(a):
Answer = math.sqrt(a)
return Answer
print(A)
root(A)
I imported the math library, which gives me access to a large amount of functions related to math, including the square root function to get the square root of a value. I ask the user to input a number, where it will then go through the function and return the square root of their value.
3.15.1 Hacks
- Write a few lines of code that implements the import function
- (Already did this but will repeat just incase)
Define what an import random function do
- Import Random essentially imports the library for random, where it basically gives the user access to a bunch of shortcuts for randomization because coding your own randomization features is too difficult and inconvenient.
List a few other things that we can import other than random
- There are a variety of other libraries that you can import besides random. For example, if we import math, math will be able to give us access to different mathematic functions so that we do not need the inconvenience of coding something like, say a square root function. Other libraries for python include:
- TensorFlow.
- NumPy.
- SciPy.
- Pandas.
- Matplotlib.
- Keras.
- SciKit-Learn.
- PyTorch. To install libraries, use "pip install _"
import math
A = int(input())
def root(a):
Answer = math.sqrt(a)
return Answer
print(A)
root(A)
3.15.2 Hacks
There is a spinner divided into eight equal parts. 3 parts of the spinner are green, two parts are blue, one part is purple, one part is red, and one part is orange. How can you simulate this situation using a random number generator.
Also answer this question: What numbers can be outputted from RANDOM(12,20) and what numbers are excluded?
- any number between 12 and 20 will be output, including 12 and 20 (tested). Any number besides those will not be included (excluded.)
import random
area = random.randint(1,8)
if area == 1:
print("Green")
elif area == 2:
print("Green")
elif area == 3:
print("Green")
elif area == 4:
print("Blue")
elif area == 5:
print("Blue")
elif area == 6:
print("Purple")
elif area == 7:
print("Red")
elif area == 8:
print("Orange")
import random
a = random.randint(12,20)
print(a)
Also answer this question: What numbers can be outputted from RANDOM(12,20) and what numbers are excluded?
- any number between 12 and 20 will be output, including 12 and 20 (tested). Any number besides those will not be included (excluded.)