3.14.1 Hacks

  • Write a program that uses a library/libraries in any sort of manner.
  • Explain your work/code
import random #random library

#This is a magic globe that will predict how many toys you will get this winter

print("How many toys will you get this Christmas?")
randomnumber = random.randint(0,100)

print("You will get ", randomnumber, " toys this Christmas")

This algorithim uses the randint function from the random library to generate a random number in the range of 0-100. The number is then printed for the user to know how many presents they will recieve.

3.15.1 Hack

  • Write a few lines of code that implements the import function

  • Define what an import random function do

  • List a few other things that we can import other than random

import random #random library

numberone = random.randint(0,2)
numbertwo = random.randint(3,4)
multiplication = numberone * numbertwo

print(numberone, " times ", numbertwo, " is ", multiplication)
2  times  3  is  6
  • An import random function uses the random library to access pre-written segments of code that randomizes numbers. In this case, a number between 0 and 2 is randomized, and a number between 3 and 4 is randomized. We can use these functions to simulate situations such as rolling dice.

  • A few other things that we can import other than random are libraries such as flask, NumPy, and pandas.

3.15.2 Hacks

  • For your hacks you need to create a random number generator that will simulate this situation:
  • 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?

import random

colorspinner =  random.randint(1,8)

if colorspinner <= 3:
    print("green")
elif colorspinner <= 5:
    print("blue")
elif colorspinner <= 6:
    print("purple")
elif colorspinner <= 7:
    print("red")
else:
    print("orange")
green

Question Answered:

RANDOM(12,20) can output numbers 12, 13, 14, 15, 16, 17, 18, 19, and 20. Any number less than 12 or greater than 20 are excluded.