# Name:
# Kerberos:
# homework_2b.py
import math
import random
##### Template for Homework 2b, exercises 4.1-4.2 ######
# ********** Exercise 4.1 **********
## 1 - decimal values
print "**************** Part 1: Fractions ****************"
print
# Generate a list of numbers 2-10 (the denominators), and print the reciprocal
for denom in range(2,11):
print 1.0 / denom # ensure float division
## 2 - countdown
print "\n**************** Part 2: Countdown ****************"
print
# Ask the user for the initial value
num = int(raw_input("Count down from which number (non-negative)? "))
# If it's is negative, print an error message
# (this behavior is your choice -- I choose to fail if it's a negative number)
# Note that if this comes after the while, the message will always print
# (because num will be -1 after the loop), so this check should come first.
if num < 0:
print "Cannot count down from a negative number."
# Otherwise, count down to 0 from the provided value
while num >= 0:
print num # print it
num -= 1 # decrement it
## 3 - exponentiation
print "\n**************** Part 3: Exponentiation ****************"
print
# Get the base and exponent from the user (and make sure they're ints)
base = int(raw_input("Base? "))
exp = int(raw_input("Exponent? "))
# Multiply the result by the base exp number of times, then print the result
# (note that we need to initialize the result variable before the loop starts
# to avoid NameErrors and incorrect behavior)
result = 1 # start with 1 in case exp is 0
for i in range(exp):
result *= base
print result
## 4 - even/odd
print "\n**************** Part 4: Divisible by 2 ****************"
print
# Get the initial number from the user
num = int(raw_input("Please enter an integer that is divisible by 2: "))
# If num isn't divisible by 2, ask the user for a new number until he/she
# enters one that is
# Note: we'll use "while num % 2 != 0" instead of using two test cases
# "if num % 2 == 1" and "elif num % 2 == 0", because this is cleaner :)
while num % 2 != 0:
num = int(raw_input("That's not even! Try again: "))
# Congratulate them! (if the code reaches here, the number must be even)
print "Congrats! You entered", num, "and it's divisible by 2 :)"
# ********** Exercise 4.2 **********
## 1 - rand_divis_3 function
def rand_divis_3():
'''
Prints a random integer from 0 to 100, and returns a boolean
stating whether it is divisible by 3 or not.
'''
random_number = random.randint(0, 100)
print 'Random int is: ', random_number
return random_number % 3 == 0
# Test Cases
print rand_divis_3()
print rand_divis_3()
print rand_divis_3()
## 2 - roll_dice function - remember that a die's lowest number is 1;
#its highest is the number of sides it has
def roll_dice(sides, rolls):
'''
Takes in two integers, sides and rolls. Simulates the "rolls" number of dice
with "sides" sides.
'''
while rolls > 0:
print random.randint(1, sides)
rolls -= 1
return "That's all!"
# Test Cases
print '\nTesting roll_dice'
print roll_dice(6, 3)
print roll_dice(20, 1)
print roll_dice(3, 2)