-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework_2a.py
More file actions
83 lines (59 loc) · 2.16 KB
/
homework_2a.py
File metadata and controls
83 lines (59 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Name: Laser Nite
# Kerberos: nite
# Date: 1/8/14
# homework_2a.py
# ********** Exercise 3.1 **********
def rock_paper_scissors(p1, p2):
choices = ["rock", "paper", "scissors"]
if p1 in choices and p2 in choices:
# If Player 1 and 2 have same selection they tie
if p1 == p2:
return "Player 1 and Player 2 tie!"
# If Player 1 is rock, Player 1 wins if Player 2 is scissors and loses if Player 2 is paper
elif p1 == "rock" and p2 == "scissors":
return "Player 1 Wins!"
elif p1 == "rock" and p2 == "paper":
return "Player 2 Wins!"
# If Player 1 is scissors, Player 1 wins if Player 2 is paper and loses if Player 2 is rock
elif p1 == "scissors" and p2 == "rock":
return "Player 2 Wins!"
elif p1 == "scissors" and p2 == "paper":
return "Player 1 Wins!"
# If Player 1 is paper, Player 1 wins if Player 2 is rock and loses if Player 2 is scissors
elif p1 == "paper" and p2 == "scissors":
return "Player 2 Wins!"
elif p1 == "paper" and p2 == "rock":
return "Player 1 Wins!"
# This else statement should be impossible to activate- a bug check
else:
return "You have discovered a bug! Email me @ nite@mit.edu"
# A player entered an invalid choice
else:
return "Invalid selection: Choose rock, paper, or scissors."
# Tests for 3.1
print rock_paper_scissors("rock","rock")
print rock_paper_scissors("paper","scissors")
print rock_paper_scissors("rock","cat")
# *********** Exercise 3.2 ***********
from math import *
## 1 - multadd function
def multadd(a, b, c):
return a * b + c
# Test Cases
print multadd(4,5,1)
print multadd(3,3,3)
print multadd(1,2,3)
## 2 - Equations
# angle_test
angle_test = str(multadd(cos(pi/4), 0.5, sin(pi/4)))
print "sin(pi/4) + cos(pi/4)/2 is: " + angle_test
# ceiling test
ceiling_test = str(multadd(2.0,log(12,7),ceil(276/19.0)))
print """ceiling(276/19) + 2 log_7(12) is:""" + ceiling_test
## 3 - yikes function
def yikes(x):
return multadd(x, e**-x, (1-e**-x)**0.5)
# Test Cases
num = 5
print "yikes(5) =", yikes(num)