-
Notifications
You must be signed in to change notification settings - Fork 1
/
menu_test.py
228 lines (190 loc) · 6.88 KB
/
menu_test.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
"""
menu_test.py
A test of the Menu class.
Classes:
MontyMenu: A menu of Monty Python skits. (Menu)
NumberMenu: A menu of integer graphs. (Menu)
TopMenu: A top level menu. (Menu)
"""
import random
import time
from menu import Menu
from cmd_example2 import Maze
class MontyMenu(Menu):
"""
A menu of Monty Python skits. (Menu)
Methods:
menu_argument: A: Have an intellectual discussion. (bool)
menu_knight: B: Get some vigorous exercise. (bool)
menu_quit: D: Stop it, that's just silly. (bool)
menu_spam: C: Enjoy some fine dining.
"""
def menu_argument(self):
"""A: Have an intellectual discussion."""
# Prep the argument.
start = time.perf_counter()
user_text = input('Please state an assertion to argue about: ')
# Argue for two minutes
while time.perf_counter() - start < 120:
# Automatically gainsay whatever the user says.
user_words = user_text.lower().split()
for negative in ('no', 'not', "isn't", "ain't", "doesn't", "wasn't"):
if negative in user_words:
user_text = input('Yes it is. ')
else:
user_text = input("No it isn't. ")
# Say goodbye.
print("I'm sorry, your five minutes is up.")
input('Press Enter to continue: ')
def menu_knight(self):
"""B: Get some vigorous exercise."""
# Set up the combat.
limbs = ['other leg', 'leg', 'other arm', 'arm']
combat = False
# Loop while the knight has limbs.
while limbs:
# None shall pass.
if not combat:
print('None shall pass.')
# Get the user's action.
user_action = input('What do you do? ')
# Attacking chops off a limb.
if user_action.lower() == 'attack':
print("Excellent attack. You chop off the black knight's {}".format(limbs.pop()))
combat = True
# Anything else after attacking provokes an attack.
elif combat:
print('The black knight attacks, but you easily block his blow.')
# Say goodbye.
input('Press Enter to call it a draw: ')
def menu_quit(self):
"""D: Stop it, that's just silly."""
return True
def menu_spam(self):
"""C: Enjoy some fine dining."""
# Get the user's order.
food = input('What would you like to eat? ')
# Prepare the meal.
pre_spam = ['spam'] * random.randint(2, 4)
post_spam = ['spam'] * random.randint(0, 2) + ['and spam.']
meal = pre_spam + [food] + post_spam
# Deliver the food and say goodbye.
print('Here is your ' + ', '.join(meal))
input('Press Enter to eat a wafer thin wafer and explode: ')
class NumberMenu(Menu):
"""
A menu of integer graphs. (Menu)
Class Attributes:
primes: All of the prime numbers up to just over 100. (list of int)
Attributes:
numbers: The number sequence generated so far. (list of int)
Methods:
menu_collatz: 3: Collatz the last number. (bool)
menu_fibonacci: 1: Add the last two numbers. (bool)
menu_prime: 2: Go up to the next prime. (bool)
menu_quit: 4: Quit. (bool)
Overridden Methods:
preloop
postchoice
postloop
"""
# All of the prime numbers up to just over 100.
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89]
primes += [97, 101]
def menu_collatz(self):
"""3: Collatz the last number."""
if self.numbers[-1] % 2:
self.numbers.append(self.numbers[-1] * 3 + 1)
else:
self.numbers.append(self.numbers[-1] // 2)
def menu_fibonacci(self):
"""1: Add the last two numbers."""
self.numbers.append(self.numbers[-1] + self.numbers[-2])
def menu_prime(self):
"""2: Go up to the next prime."""
self.numbers.append([p for p in self.primes if p > self.numbers[-1]][0])
def menu_quit(self):
"""4: Quit."""
return True
def preloop(self):
"""Processing done before starting the choice/action loop. (None)"""
# Set up number lists.
self.numbers = [0, 1]
# Show the starting number.
self.status = 'The number is now {}.'.format(self.numbers[-1])
def postchoice(self, stop, choice):
"""
Common processing after the choice is proccessed. (bool)
Parameters:
stop: Flag for stopping the menu loop. (bool)
choice: The user's choice. (str)
"""
# Show the current number.
if not self.status:
self.status = 'The number is now {}.'.format(self.numbers[-1])
# Check the current number.
if self.numbers[-1] > 99:
return True
else:
return stop
def postloop(self):
"""Processing done after the choice/action loop ends. (None)"""
print('The final number is {}.'.format(self.numbers[-1]))
print('Have a nice day.')
def sort_menu(self, menu_lines):
"""
Sort the lines of the menu text. (None)
Parameters:
menu_lines: the lines of the menu. (list of str)
"""
menu_lines.sort(key = lambda line: int(line.split(':')[0]))
class TopMenu(Menu):
"""
A top level menu. (Menu)
Class Attributes:
rps_wins: What beats what in rock-paper-scissors. (dict of str: str)
Methods:
menu_maze: A: Play in a maze. (bool)
menu_numbers: B. Play with numbers. (bool)
menu_rps: C: Play with your hands. (bool)
menu_quit: E: Quit. (bool)
menu_words: D: Play with words. (bool)
"""
# What beats what in rock-paper-scissors.
rps_wins = {'rock': 'scissors', 'paper': 'rock', 'scissors': 'paper'}
def menu_maze(self):
"""A: Play in a maze."""
maze = Maze()
maze.cmdloop()
def menu_numbers(self):
"""B: Play with numbers."""
numbers = NumberMenu()
numbers.menuloop()
def menu_rps(self):
"""
C: Play with your hands.
This is just a game of rock-paper-scissors.
"""
while True:
play = input('Rock, paper, or scissors? ').lower()
bot = random.choice(list(self.rps_wins.keys()))
if play not in self.rps_wins:
print("Invalid play. Come on, this is kid's stuff.")
elif play == bot:
print('Draw, play again.')
elif self.rps_wins[play] == bot:
print('I chose {}. You won!'.format(bot))
break
else:
print('I chose {}. You lose.'.format(bot))
break
def menu_quit(self):
"""E: Quit."""
return True
def menu_words(self):
"""D: Play with words."""
words = MontyMenu()
words.menuloop()
if __name__ == '__main__':
top = TopMenu()
top.menuloop()