#Evan McNeill
#November 3, 2021
#U3M1A1.1
#Import different Python modules
import math
x = -5
math.fabs(x)
import math as ml
y = 12
ml.fabs(y)
from math import fabs
fabs(-5)
# [ ] Import the math module and use an appropriate function to find the greatest common divisor of 16 and 28
import math
math.gcd(16,28)
# [ ] Prompt the user to input 2 positive integers then print their greatest common divisor
import math
x=int(input("Please enter a number: "))
y=int(input("Please enter a number: "))
#had to cast the string to a number so this could work
math.gcd(x,y)
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
def is_even(n):
if (x%2)==0:
return True
else:
return False
# Test the function
x = 5
if is_even(x):
print("Number is even")
else:
print("Number is odd")
# [ ] Use the function is_even to print the square root of all the even numbers in the following list
l = [25, 34, 193, 2, 81, 26, 44]
def is_even(n):
return (n % 2) == 0
from math import sqrt
for n in l:
if is_even(n):
print(sqrt(n))
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
(4 + 16) / 2
#added parenthesis around 4+16
# [ ] Correct the following expression so the answer is 250; review the operator precedence table and use only one () pair
2 * (3 + 2) ** 3
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
import math
x = 75.34
print(math.trunc(x))
print(math.floor(x))
print(math.ceil(x))
# [ ] 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
print("Total price: ", 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
from random import randrange
def die_roller ():
return (randrange(1, 7))
print(die_roller())
# [ ] Modify the odd_random() function to use randint instead of randrange
from random import randint
def odd_random():
return (randint(1,102))
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[die_roller(),die_roller()]
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
from random import shuffle,randint
def pink_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)
t = card_type[randint(0,3)]
n = card_number[randint(0,12)]
return [n,t]
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"]
from random import choice
def citieschoice():
options = cities
return(choice(options))
print(citieschoice());