import math
x = -5
math.fabs(x)
import math as ml
y = 12
ml.fabs(y)
from math import fabs, sqrt
fabs(-5)
# [ ] Import the math module and use an appropriate function to find the greatest common divisor of 16 and 28
# Make sure you look in the math module docs in the link from the previous block, or look
# up the math module on w3schools
import math
math.fabs(16)
math.fabs(28)
# [ ] Prompt the user to input 2 positive integers then print their greatest common divisor
nums_value_one = input("enter a number: ")
nums_value_two = input("enter another number")
math.fabs(int(nums_value_one))
math.fabs(int(nums_value_two))
from math import sqrt
a = 3
b = 4
h = sqrt(a ** 2 + b ** 2)
print(h)
# Even number (print 0)
print(102 % 2)
# Odd Number (print 1)
print(77 % 2)
# [ ] Fill out the function is_even with a code block that returns True if n is even and returns False if n is odd
x = float(input("Put in a number"))
def is_even(n):
# add your code to this part
n = x % 2
if n == 0:
print("True")
else:
print("False")
is_even(x)
# [ ] Use the function is_even to print the square root of all the even numbers in the following list
x = float(input("Put in a number"))
def is_even(n):
# add your code to this part
n = x % 2
if n == 0:
n = sqrt(x)
else:
pass
print(n)
is_even(x)
5 + 2 * 8
(5 + 2) * 8
12 / 4 * 3
# Method 1
12 / (4 * 3)
# Method 2
12 / 4 / 3
# [ ] Correct the following expression so the answer is 10
2 + 16 // 2
# [ ] Correct the following expression so the answer is 250; review the operator precedence table and use only one () pair
2 * 5 * (2 + 3) ** 2
from math import floor, ceil, trunc
prize = 213
option1 = floor(213 / 2)
print("You get", option1, " and your friend gets ", prize - option1)
from math import floor, ceil, trunc
prize = 213
option2 = ceil(213 / 2)
print("You get", option2, " and your friend gets ", prize - option2)
# [ ] Use an appropriate rounding function to round 75.34 to 75 and then to 76
num = math.floor(75.34)
num = math.ceil(75.34)
print(num)
# [ ] Use an appropriate rounding function to fix the following `float` error
# Price of a chocolate box
p = 4.35
# Quantity needed
q = 200
# Order total price (Should be 4.35 * 200 = $870.00)
total = p * q
rnd_total = math.ceil(total)
print("Total price: ", rnd_total)
from random import randint
print(randint(1, 10))
from random import randrange
print(randrange(1, 11))
from random import randrange
print(randrange(1, 11, 2))
from random import randint
def die_roller ():
return (randint(1, 6))
# roll a die
print(die_roller())
from random import randrange
def odd_random():
return (randrange(1, 102, 2))
# Generate an odd random integer
print(odd_random())
# [ ] Modify the die_roller() function to use randrange instead of randint
def die_roller():
return(randrange(1,7))
print(die_roller())
# [ ] Modify the odd_random() function to use randint instead of randrange
def odd_random():
return(randint(1,101))
print(odd_random())
# [ ] Complete the function dice_roller() so it rolls 2 dice
# Use the die_roller function
from random import randint
def die_roller():
return(randint(1, 6))
def dice_roller():
return(randint(1,101))
print(dice_roller())
from random import choice
# Select Rock, Paper, or Scissors
def RPS():
options = ['Rock', 'Paper', 'Scissors']
# return one of the elements at random
return (choice(options))
# Generate an option
print(RPS());
from random import shuffle
x = ['Ana', 'John', 'Mike', 'Sally']
shuffle(x)
print(x)
from random import choice
def pick_card():
card_type = ['Clubs', 'Diamonds', 'Hearts', 'Spades'];
card_number = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']
# choose a type at random
t = choice(card_type)
n = choice(card_number)
return [n, t]
# Show the randomly picked card
print(pick_card())
# [ ] Modify the pick_card() function to use `shuffle` instead of choice
def pick_card():
card_type = ['Clubs', 'Diamonds', 'Hearts', 'Spades'];
card_number = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']
shuffle(card_type)
shuffle(card_number)
return [card_type, card_number]
print(pick_card())
# [ ] The following list contain the 10 most populous American cities; write code to randomly select one of the cities to visit
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia", "San Antonio", "San Diego", "Dallas", "San Jose"]
vacation = choice(cities)
print(vacation)