-
Notifications
You must be signed in to change notification settings - Fork 0
/
project1.py
93 lines (78 loc) · 2.3 KB
/
project1.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
import random
import time
def rps():
print("Rock Paper Scissors")
player = input("Enter either \"r\" for rock, \"p\" for paper, or \"s\" for scissors \n ")
comp = ["rock", "paper", "scissors"]
options = random.choice(comp)
if player.lower() not in ('r', 's', 'p'):
print("Please enter the correct input!")
else:
rps_result(player, options)
main()
def rps_result(player, options):
if player == "r" and options == "scissors":
print("Computer choose \"scissors\" \n you Win!")
if player == "p" and options == "rock":
print("Computer choose \"rock\" \n you Win!")
if player == "s" and options == "paper":
print("Computer choose \"paper\" \n you Win!")
if(player == "r" and options == "rock") or (player == "p" and options == "paper") or (player == "s" and options == "scissors"):
print("It's a tie!")
else:
print("Computer choose " + options + " you lose!")
def number():
num = input("Enter a number to see if it's a perfect number below: \n")
mylist = factors(int(num))
pointer = mylist[0]
for i in range(1, len(mylist)-1):
pointer = pointer + mylist[i]
if(pointer == num):
print(str(num) + " is a perfect number!")
else:
print(str(num) + " is a not perfect number!")
main()
def factors(n):
mylist = []
for i in range(1, n+1):
if n % i == 0:
mylist.append(i)
return mylist
main()
def mcarlo():
counter = 0
ntrials = 10**7
for i in range(ntrials):
x = random.uniform(-1, 1)
y = random.uniform(-1, 1)
result = x**2 + y**2
if result <= 1:
counter += 1
else:
continue
pi_estimate = 4 * counter/(1.0*ntrials)
print("Trials")
print(ntrials)
print("Pi Estimation")
print("%f" % pi_estimate)
print("Time Taken")
print(time.time())
print("\n")
main()
def error():
print("Goodbye!")
switch = {
"1": rps,
"2": number,
"3": mcarlo,
}
def main():
print("CS 299 Project 1")
print("1) Rock Paper Scissors Game")
print("2) Perfect Numbers")
print("3) Monte Carlo")
print("Press any other key to exit")
choice = input("Enter a choice: \n")
switch.get(str(choice), error)()
if __name__ == "__main__":
main()