#ConnerKahn
#11/25
#I learned more about math in python and its capabilities
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 = input("Input an integer x")
y = input("Input an integer y")
math.gcd(int(x), int(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):
#TODO
if (n%2):
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
#TODO
for num in l:
if is_even(num):
print(sqrt(num))
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
# [ ] 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
from math import ceil, floor
x = 75.34
round(x)
ceil(x)
# [ ] Use an appropriate rounding function to fix the following `float` error
from math import ceil
# Price of a chocolate box
p = 4.35
# Quantity needed
q = 200
# Order total price (Should be 4.35 * 200 = $870.00)
total = ceil(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, 6))
print(die_roller())
from random import randint
import math
def odd_random():
x = 0
while x % 2 == 0:
x = randint(1,102)
if x % 2 != 0:
break
return (x)
# Generate an odd random integer
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():
x = die_roller()
y = die_roller()
return(x, y)
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
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_number)
shuffle(card_type)
# choose a type at random
t = choice(card_type)
n = choice(card_number)
return [n, t]
# Show the randomly picked card
print(pick_card())
# [ ] The following list contain the 10 most populous American cities; write code to randomly select one of the cities to visit
from random import shuffle
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia", "San Antonio", "San Diego", "Dallas", "San Jose"]
shuffle(cities)
print(cities[0])