# My name is Pavan Kumpatla and today's date is November 28, 2021.
# I learned how to import and import from, I learned "ceil, floor, trunc"
# I also learned how to import from the "random" and the different functions in it
# I had no difficulty
import math
# imports math
x = -5
# stores value
math.fabs(x)
# gives absolute value in float form of x
import math as ml
# import math and rename it as ml
y = 12
# stores value
ml.fabs(y)
# gives absolute value in float form of y
from math import fabs
# from math, import fabs
fabs(-5)
# uses function, converts -5 to absolute value in float form
# [ ] Import the math module and use an appropriate function to find the greatest common divisor of 16 and 28
import math
# imports math
print(math.gcd(16, 28))
# prints, finds greatest common divisor of 16 and 28
# [ ] Prompt the user to input 2 positive integers then print their greatest common divisor
import math
user1 = int(input("Type In The 1st Positive Integer:- "))
user2 = int(input("Type In The 2nd Positive Integer:- "))
print("Greatest Common Divider:- ")
print(math.gcd(user1, user2))
from math import sqrt
# from math, import square root
a = 3
# contains value
b = 4
# contains value
h = sqrt(a ** 2 + b ** 2)
# a to the power of 2 plus b to the power of 2
# square root the total
# and store it
print(h)
# prints
# Even number (print 0)
print(102 % 2)
# 102/2 leaves nothing behind
# Odd Number (print 1)
print(77 % 2)
# 77/2 leaves a 1 behind (77-76 = 0)
# [ ] 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):
# creates new function with a parameter
if n % 2 == 0:
# if the remainder of n/2 is 0
return True
# return True
else:
# else
return False
# return False
# I did "return" because I don't want the answer to repeat itself
# 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]
# contains list
def is_even(n):
# creates new function with parameter
return (n % 2) == 0
# returns n/2 equals 0
from math import sqrt
# from math, import square root
for number in l:
# for number in l
if is_even(number):
# if it is even
root = sqrt(number)
# find square root of number and store it
print(root)
# prints
else:
# else
pass
# pass
5 + 2 * 8
# multiplies first, then adds
(5 + 2) * 8
# adds first, then multiplies
12 / 4 * 3
# this is the wrong answer
# Method 1
12 / (4 * 3)
# multiplies, then adds
# Method 2
12 / 4 / 3
# divides
# [ ] Correct the following expression so the answer is 10
(4 + 16) / 2
# adds, divides
# [ ] Correct the following expression so the answer is 250; review the operator precedence table and use only one () pair
2 * (3 + 2) ** 3
# adds, raises to the power of 3, multiplies
from math import floor, ceil, trunc
# from math, import floor, ceil, trunc
# only gets these instead of the whole library
prize = 213
# contains value
option1 = floor(213 / 2)
# divides 213 by 2 and rounds down and then stores it
print("You get", option1, " and your friend gets ", prize - option1)
# prints, subtracts
from math import floor, ceil, trunc
# from math, import floor, ceil, trunc
# only gets these instead of the whole library
prize = 213
# contains value
option2 = ceil(213 / 2)
# divides 213 by 2 and rounds up and then stores it
print("You get", option2, " and your friend gets ", prize - option2)
# prints, subtracts
# [ ] Use an appropriate rounding function to round 75.34 to 75 and then to 76
import math
# imports math
x = 75.34
# stores math
print(math.floor(x))
# rounds down, prints
print(math.ceil(x))
# rounds up, prints
# [ ] Use an appropriate rounding function to fix the following `float` error
from math import ceil
# Price of a chocolate box
p = 4.35
# stores value
# Quantity needed
q = 200
# stores value
# Order total price (Should be 4.35 * 200 = $870.00)
total = p * q
print("Total price: ", ceil(total))
# I editted this part to round up
from random import randint
# from random, import random integer function
print(randint(1, 10))
# print a random integer between 1-10 including 1 and 10
from random import randrange
# from random, import random range (function)
print(randrange(1, 11))
# print something random between 1-11 NOT including 11
from random import randrange
# from random, import random range (function)
print(randrange(1, 11, 2))
# print something random between 1-11 NOT including 11 WITH an interval of 2 (odd numbers)
from random import randint
# from random, import random integer (function)
def die_roller ():
# creates new function
return (randint(1, 6))
# returns a random integer between 1-6 including 1 and 6
# roll a die
print(die_roller())
# prints, calls the function
from random import randrange
# from random, import random range (function)
def odd_random():
# creates new function
return (randrange(1, 102, 2))
# returns a random integer between 1-102 NOT including 102 WITH an interval of 2 (odd numbers)
# Generate an odd random integer
print(odd_random())
# prints
# [ ] Modify the die_roller() function to use randrange instead of randint
from random import randrange # Editted this part
# from random, import random range (function)
def die_roller():
# creates new function
return (randrange(1, 7)) # Editted this part
# returns a random integer between 1-6 NOT including 7
# roll a die
print(die_roller())
# prints, calls the function
# [ ] Modify the odd_random() function to use randint instead of randrange
from random import randint
# from random, import random integer (function)
def odd_random():
# creates new function
return (randint(0, 102))
# returns a random integer between 0-102 including 102
# Generate an odd random integer
print(odd_random())
# prints
# [ ] Complete the function dice_roller() so it rolls 2 dice
# Use the die_roller function
from random import randint
# from random, import random integer (function)
def die_roller():
# creates new function
return(randint(1, 6))
# returns a random integer between 1 and 6 including 6
def dice_roller():
# creates new function
dice1 = randint(1, 6)
# stores a random integer between 1 and 6 including 6
dice2 = randint(1, 6)
# stores a random integer between 1 and 6 including 6
return ("Dice 1:-", dice1, "Dice 2:- ", dice2)
# returns
print(dice_roller())
# prints, calls function
from random import choice
# from random, import choice (function)
# Select Rock, Paper, or Scissors
def RPS():
# creates new function
options = ['Rock', 'Paper', 'Scissors']
# stores values in list
# return one of the elements at random
return (choice(options))
# Generate an option
print(RPS())
# prints, calls function
# NOTE:- I got rid if the ";" at the end. It was originally "print(RPS());"
from random import shuffle
# from random, import shuffle (function)
x = ['Ana', 'John', 'Mike', 'Sally']
# stores value in list
shuffle(x)
# shuffles values in x
print(x)
# prints
from random import choice
# from random, import choice
def pick_card():
# creates new function
card_type = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] # got rid of the ";"
# stores value in list
card_number = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']
# stores value in list
# choose a type at random
t = choice(card_type)
# stores random object
n = choice(card_number)
# stores random object
return [n, t]
# returns
# Show the randomly picked card
print(pick_card())
# prints, calls function
# [ ] Modify the pick_card() function to use `shuffle` instead of choice
from random import shuffle, randint
# from random, import choice
def pick_card():
# creates new function
card_type = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] # got rid of the ";"
# stores value in list
card_number = ['Ace', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'Jack', 'Queen', 'King']
# stores value in list
# choose a type at random
shuffle(card_type)
# stores
shuffle(card_number)
# stores
t = card_type[randint(0, 3)]
# gets random integer between 0-3
n = card_number[randint(0, 12)]
# gets random integer between 0-12
return [n, t]
# returns
# Show the randomly picked card
print(pick_card())
# prints, calls function
# [ ] The following list contain the 10 most populous American cities; write code to randomly select one of the cities to visit
from random import choice
# from random, import choice (function)
cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia", "San Antonio", "San Diego", "Dallas", "San Jose"]
# contains value in list
print(choice(cities))
# prints, chooses random object