-
Notifications
You must be signed in to change notification settings - Fork 0
/
HW3.py
168 lines (135 loc) · 5.92 KB
/
HW3.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Your name: Haley Johnson
# Your student id:2694 6938
# Your email: [email protected]
# List who you have worked with on this homework:
# import the random module for use in this program
import random
# Create the class Magic_8
class Magic_8:
def __init__(self, possible_answers):
self.answer_list = possible_answers
self.question_list = []
self.answer_history_list = []
# create the constructor (__init__) method
# it takes as input: a list of possible answers
# it sets this object's answer_list (instance variable) to the passed list of possible answers
# it sets this object's question_list (instance variable) to the an empty list
# it sets this object's answer_history_list (instance variable) to an empty list
def __str__(self):
return ", ".join(self.answer_list)
# create the __str__ method
# It should return a string with all the possible answers
# in answer_list separated by commas
# For example : "Yes, No, Not clear"
def shake_ball(self):
idx = random.randint(0, len(self.answer_list) - 1)
response = self.answer_list[idx]
self.answer_history_list.append(response)
return response
# create the shake_ball method
# it randomly picks an index from 0 to the number of items in the answer_list minus one
# it adds that index to the end of the answer_history_list
# it returns the answer at the picked index
def check_question(self, question):
if question in self.question_list:
return "I already answered that question!"
else:
self.question_list.append(question)
return self.shake_ball()
# create the check_question method that takes a question
# it checks if the question is in the question_list and if so returns
# "I already answered that question!”
# Otherwise it adds the question to the question_list and
# returns the answer from shake_ball
def print_history(self):
idx = 0
if len(self.answer_history_list) == 0:
print("None yet")
else:
for item in self.answer_history_list:
answer = self.answer_history_list[idx]
question = self.question_list[idx]
print("{} {} - {}".format(idx, answer, question))
idx += 1
# create the print_history method
# prints "[answer index] question - answer" for each of the indices in the answer_history_list
# from the first to the last with each on a single line. If there are not items in the
# answer_history_list it prints "None yet"
# it does not return anything!
# EXTRA POINTS
# create the generate_n_responses method
# it takes as input:
# a number, n. Ex: 200
# it generates a random response n times by calling shake_ball
# then returns the index and length of the longest consecutive run
# It should reset the answer_history_list to an empty list first.
# A run is a repetition of the same number consecutively in a list.
# Ex: If 10 random answers are [1,5,6,3,2,4,1,4,4,4] then three 4's is the longest run
# hence the function should return "longest run was length of 3 for index 4
def generate_n_responses(self, num):
self.answer_history_list = []
hist_list = []
for i in range(num):
response = self.shake_ball()
idx = self.answer_list.index(response)
print ("Rolled a: {}, i.e. {}".format(response, idx))
hist_list.append(idx)
last = -1
longest_run_val = -1
longest_run = 0
current_run = 0
for item in hist_list:
if item == last:
current_run += 1
else:
current_run = 1
last = item
if current_run > longest_run:
longest_run_val = last
longest_run = current_run
return "longest run was length of {} for index {}".format(longest_run, longest_run_val)
def main():
# You are welcome to replace the answer_list with your desired answers
answer_list = ["Yes", "No", "Ask again", "Maybe", "Not clear", "Ye", "Most certainly"]
bot = Magic_8(answer_list)
# get the first question or quit
question = input("Ask a question or type quit: ")
# loop while question is not "quit"
# get an answer from check_question
# print question - answer
# get the next question or quit
while question.lower() != "quit":
answer = bot.check_question(question)
print("{} - {}".format(question, answer))
question = input("Ask a question or type quit: ")
def test():
answer_list = ["Yes", "No", "Ask again", "Maybe", "Not clear"]
print()
print("Testing Magic 8 Ball:")
bot2 = Magic_8(answer_list)
print("Testing the __str__ method")
print(bot2)
print()
print("Printing the history when no answers have been generated yet")
bot2.print_history()
print()
print("Asking the Question: Am I hungry?")
print(bot2.check_question("Am I hungry?"))
print()
print("Asking the Question: Am I hungry? again")
print(bot2.check_question("Am I hungry?"))
print()
print("Asking the Question: Should I go for a walk?")
print(bot2.check_question("Should I go for a walk"))
print()
print("Printing the history")
bot2.print_history()
print()
#EXTRA POINTS
#Uncomment the lines below if you attempt the extra credit!
print("Testing generate_n_responses method with 200 responses")
print(bot2.generate_n_responses(200))
# only run the main function if this file is being run (not imported)
if __name__ == "__main__":
main()
test() #- uncomment when you are ready to test your magic 8 ball