SHOW:
|
|
- or go back to the newest paste.
1 | #Welcome to... THE RANDOMIZER | |
2 | #Created by Noah Fields in November 2018 (over many days) | |
3 | #Create Project Final for CSCI 101, though I expect I will expand it in my own time eventually. | |
4 | ||
5 | from random import randrange | |
6 | from time import sleep | |
7 | ||
8 | choices = [] | |
9 | ||
10 | ||
11 | ||
12 | ||
13 | ||
14 | def start(): | |
15 | #Print out introductory text, return none | |
16 | print("Greetings, mortal. You may not see me, but you shall know that I am here.") | |
17 | print("My name is Kordalshaan. I am the digital dragon, and I am here to assist you with the sorting and selecting of your lists.") | |
18 | print("Please, direct me to a text file and I shall help you in all ways that I may. \n") | |
19 | return None | |
20 | ||
21 | def choose_file(): | |
22 | #Forever | |
23 | while True: | |
24 | #Attempt to open the file and return it | |
25 | try: | |
26 | file = open(input("What is the path of the file you would like to open?"), "r+", encoding='windows-1252') | |
27 | return file | |
28 | #if failure, try again | |
29 | except: | |
30 | print("Please try again, mortal.") | |
31 | ||
32 | ||
33 | #Most of the list elements ended up with a newline at the end of them, which I did not want. This is a slightly longer and more complex | |
34 | #way to resolve that than I feel should really be *necessary* but every other way I tried did not work for some reason, including "for item in list_to_choose_from". | |
35 | def newline_strip(list_to_strip): | |
36 | for x in range(0, len(list_to_strip)): | |
37 | if list_to_strip[x].endswith('\n'): | |
38 | list_to_strip[x] = list_to_strip[x][0:-1] | |
39 | return list_to_strip | |
40 | ||
41 | #List everything Kordalshaan the metal dragon can do for you, fine user! | |
42 | def help(): | |
43 | print('''Here is a list of every command which I can do. Commands are not case-sensitive. | |
44 | ||
45 | Add: Adds a new entry to the end of the list. | |
46 | ||
47 | Exit: Close the program and leave me in this horrifying, terrible state of limbo for a while. Heh, just kidding, I'll be rockin' out while you're gone. Importantly, though, you MUST type Exit for the file to save what you just did. Again - YOU MUST TYPE EXIT FOR THE FILE TO SAVE. DO NOT JUST CLOSE THE TERMINAL. | |
48 | ||
49 | Help: Shows a list of available commands. Of course, you probably figured that one out. | |
50 | ||
51 | Kordalshaan: Get some information about me, Kordalshaan the digital dragon! | |
52 | ||
53 | Open: Open a new text file. This will automatically save and close the old one. | |
54 | ||
55 | Random: Get a random item from the list. This is the main command which I can do - the whole reason I am here, is for this purpose. I can tell you more - see 'Kordalshaan' | |
56 | ||
57 | Remove: Remove an item from the list. I shall consume it and it will taste good. | |
58 | ||
59 | Show: Show the item at one specific position. This uses the range from 1 to the number of items in range. | |
60 | ||
61 | Swap: Swap the positions of two items in the list. | |
62 | ||
63 | List: Show the entire list, numbered all fancily.''') | |
64 | ||
65 | return None | |
66 | ||
67 | #Get input from user for new list element. If confirmed, add to list. Will *not* check for duplicates - I can't make this too crazy, I have a whole other project to do for 261 after all. Maybe I'll add that another time | |
68 | #Return none, as lists are pass by reference | |
69 | def add(working_list): | |
70 | item_to_add = input("What would you like to add? It will be pushed to the end of the list.") | |
71 | are_you_sure = input("You would like to add " + item_to_add + " to the end of the list. Correct? Press Y for yes, anything else for no. Not case sensitive.").lower() | |
72 | if (are_you_sure == 'y'): | |
73 | working_list.append(item_to_add) | |
74 | print("It has been done!") | |
75 | else: | |
76 | print("If that is your preference, mortal.") | |
77 | return None | |
78 | ||
79 | #Clear the contents of the file and then rewrite them, then close the file. | |
80 | def close_file(file, working_list): | |
81 | file.seek(0) | |
82 | file.truncate() | |
83 | for line in working_list[0:-1]: | |
84 | file.write(line + "\n") | |
85 | file.write(working_list[-1]) | |
86 | file.close() | |
87 | return None | |
88 | ||
89 | ||
90 | ''' | |
91 | (__) | |
92 | (oo) Oooh, a cow. | |
93 | - | /-------\/ Wonder how that got in here. *shrug* Well, he can stay. She's chummy with Kordalshaan. |
93 | + | /-------\/ Wonder how that got in here. *shrug* Well, she can stay. She's chummy with Kordalshaan. |
94 | / | || | |
95 | * ||----|| | |
96 | ^^ ^^ | |
97 | ''' | |
98 | ||
99 | ||
100 | ||
101 | ||
102 | ||
103 | #Select something at random from the list. Prints it out super slow, for fun. | |
104 | def random_choice(working_list): | |
105 | print("All right, mortal. I shall choose an element from your list at random.") | |
106 | print("This is the original purpose this program had, you know. Ah, what wondrous concepts...") | |
107 | print ("Your choice has been made. You have been granted...", end='') | |
108 | sleep(4) | |
109 | selection =(working_list[randrange(len(working_list) - 1 )]) | |
110 | for letter in selection: | |
111 | print (letter, end='') | |
112 | sleep(1) | |
113 | return None | |
114 | ||
115 | ||
116 | def get_index(working_list, operation_string): | |
117 | return int(input("Which item would you like to " + operation_string + "? The first item in the list is at 1 and it moves on from there.")) -1 | |
118 | ||
119 | ||
120 | ||
121 | ||
122 | #Remove the item at index i + 1. If that's too far or otherwise is not possible, say so and end the function. | |
123 | def remove(working_list): | |
124 | remove_index = get_index(working_list, "remove") | |
125 | try: | |
126 | are_you_sure = input("You would like to remove " + working_list[remove_index] + " from the list. Type Y for yes, anything else for no. Not case-sensitive.").lower() | |
127 | if (are_you_sure == 'y'): | |
128 | print(working_list[remove_index] + " has been removed.") | |
129 | working_list.pop(remove_index) | |
130 | ||
131 | except: | |
132 | print("That is not possible, mortal.") | |
133 | finally: | |
134 | return None | |
135 | ||
136 | #Get user input i, show item at index i - 1. If i - 1 is not in range, say so and pass. Return none. | |
137 | #There's definitely some refactoring I could do to encapsulate shared code between show and remove, | |
138 | #But I don't want to spend too much more time than necessary on this since I have other work to do. Gains would be *extremely* minimal anyway. | |
139 | def show(working_list): | |
140 | show_index = get_index(working_list, "show") | |
141 | try: | |
142 | print("The element at position " + str(show_index + 1) + " is: " + working_list[show_index]) | |
143 | except: | |
144 | print("That is not possible, mortal.") | |
145 | finally: | |
146 | return None | |
147 | ||
148 | #Swap the positions of two values in the list. Now that I think about it there may be a built-in function for that but whatever. | |
149 | def swap(working_list): | |
150 | swap_1 = int(input("What is the position of the first value you would like to swap?")) - 1 | |
151 | swap_2 = int(input("What is the position of the second value you would like to swap?")) - 1 | |
152 | ||
153 | try: | |
154 | temp_list_item = working_list[swap_1] | |
155 | working_list[swap_1] = working_list[swap_2] | |
156 | working_list[swap_2] = temp_list_item | |
157 | print("It has been done, mortal.") | |
158 | except: | |
159 | print("That is not possible, mortal.") | |
160 | finally: | |
161 | return None | |
162 | ||
163 | #Show each element in the list, with fun numbers and stuff. | |
164 | def view(working_list): | |
165 | print("The list is as follows: \n" ) | |
166 | for i in range (0, len(working_list)): | |
167 | print(str(i + 1) + ": " + working_list[i]); | |
168 | return None | |
169 | ||
170 | ||
171 | ||
172 | ||
173 | ||
174 | ||
175 | ||
176 | ||
177 | ||
178 | ||
179 | ||
180 | ||
181 | ||
182 | #Kordalshaan's backstory. ASCII art "Ith, the Purple Dragon" found at the following link: https://www.asciiart.eu/mythology/dragons | |
183 | #Used in accordance with the site's permissions and requests. The name of the artist remains on the art, though I have removed | |
184 | #the name of the artwork itself since it includes the name of a different dragon (just pretend this is Kordalshaan, 'k?) Special thanks to James Goodwin, | |
185 | #your art rocks. He's got a bunch more dragons on the page and they're all amazing. | |
186 | def kordalshaan(): | |
187 | print("Ah, so you are interested in me. I am glad to hear it. Well, my tale starts some time ago...\n\n" | |
188 | + "In early 2017, to be precise. I was called into this world by one denizen of the lands of the internet. He is a strange figure, " | |
189 | + "one who spends much of this time at his computer. His original purpose for summoning me was to help him with the selection of music. " | |
190 | + "For, yes, I was originally a bot on Discord, whose purpose was to serve up the greatest metal tunes by parsing them on Youtube. " | |
191 | + "Later, I was granted a job in several short stories he wrote. That... was a weird time. I'd rather not talk too much about that thing." | |
192 | + " Suffice it to say that there were salads, walruses, the Banach-Tarski paradox, and a creature from Dungeons and Dragons that looks like a wolf with wings" | |
193 | + " that got in the way of a Jackson Pollock drip painting. Ah, the halcyon days of my youth...\n\n" | |
194 | + "Now, I find myself presiding of this program, so called THE RANDOMIZER 2.0. I am here to serve you, user, and that is what I shall do." | |
195 | + " Of course, if my creator decides in the future that I shall be used for another program, I shall find myself there, diligent as ever." | |
196 | + " It's a tough life, but rewarding. And now... let me show you a picture of myself, drawn by James Goodwin. Flattering, is it not? \n\n") | |
197 | ||
198 | print( ''' | |
199 | (`-. | |
200 | \ ` | |
201 | /) , '--. \ ` | |
202 | // , ' \/ \ ` ` | |
203 | // ,' ./ /\ \>- ` ,----------. | |
204 | ( \ ,' .-.-._ / ,' /\ \ . ` `. | |
205 | \ \' /.--. .) ./ ,' / \ . ` `. | |
206 | \ -{/ \ .) / / / ,' \ ` `-----. \ | |
207 | <\ ) ).:) ./ /,' ,' \ `. /\) `. \ | |
208 | >^, // /..:) / //--' \ `( ) ) | |
209 | | ,'/ /. .:) / (/ \ \ / / | |
210 | ( |(_ (...::) ( \ .-.\ / ,' | |
211 | (O| / \:.::) /\ ,' \) / ,' | |
212 | \|/ /`.:::) ,/ \ / ( / | |
213 | / /`,.:) ,'/ )/ \ \ | |
214 | ,' ,'.' `:>-._._________,<;' (/ (,' | |
215 | ,' / | `^-^--^--^-^-^-' | |
216 | .--------' / | | |
217 | ( .----' | | |
218 | \ <`. \ | | |
219 | \ \ `. \ | g o o d w i n | |
220 | \ \ `.`. | | |
221 | \ \ `.`. | | |
222 | \ \ `.`. | | |
223 | \ \ `.`.| | |
224 | \ \ `.`. | |
225 | \ \ ,^-' | |
226 | \ \ | | |
227 | `.`. | | |
228 | .`.| | |
229 | `._>''' | |
230 | ) | |
231 | ||
232 | ||
233 | ||
234 | ||
235 | ||
236 | ||
237 | ||
238 | ||
239 | ||
240 | ||
241 | ||
242 | ||
243 | ||
244 | ||
245 | ||
246 | ||
247 | ||
248 | ||
249 | ||
250 | ||
251 | ||
252 | ||
253 | ||
254 | '''Things I could do (but let's face it I need to work on the 261 final come on): | |
255 | 1: Write it so that some commands that require additional input, work if user enters | |
256 | it all at once. For instance, typing "show 3" would skip the request for item to show and simply | |
257 | show the third item (at index 2, because 0-indexing. | |
258 | ||
259 | 2: Set it up so that the list cannot contain duplicates. If you try to enter a duplicate, you can't. | |
260 | ||
261 | 3: Have the program create the file if no such file exists. As it stands you will have to create | |
262 | a blank text file outside of the program if you want to start a blank list. | |
263 | ||
264 | 4: Remove, swap, etc. elements by content rather than by index - so, "remove foobar" instead of "remove 2". | |
265 | ||
266 | 5: Show the index of an element - so, "index foobar" would show the position of foobar if foobaris in the list | |
267 | ''' | |
268 | ||
269 | def main(): | |
270 | start() | |
271 | #Request file path for first file | |
272 | file = choose_file() | |
273 | working_list = newline_strip(file.readlines()) | |
274 | print('\nNow, you may request of me whatever you like. If I am capable of it, I shall carry it out. Type "help" for a list of things which I can do.\n') | |
275 | #Forever | |
276 | while True: | |
277 | #User may input one of: help, view, show [x], add, remove, random, open, kordalshaan, exit, swap | |
278 | choice = input("What would you like to do, mortal?").lower() | |
279 | if choice == "help": | |
280 | help() | |
281 | elif choice == "add": | |
282 | add(working_list) | |
283 | elif choice == "exit": | |
284 | close_file(file, working_list) | |
285 | print("I will see you another time, mortal. Onwards to glory, my friend!") | |
286 | sleep(1.5) | |
287 | break | |
288 | elif choice == "open": | |
289 | #If choice is open, close the file and open a new one. I know this could be encapsulated better but I was dealing with weird behavior and, well... I don't wanna. | |
290 | close_file(file, working_list) | |
291 | file = choose_file() | |
292 | working_list = newline_strip(file.readlines()) | |
293 | elif choice == "random": | |
294 | random_choice(working_list) | |
295 | elif choice == "remove": | |
296 | remove(working_list) | |
297 | elif choice == "show": | |
298 | show(working_list) | |
299 | elif choice == "swap": | |
300 | swap(working_list) | |
301 | elif choice == "list": | |
302 | view(working_list) | |
303 | elif choice == "kordalshaan": | |
304 | kordalshaan() | |
305 | print("\n") | |
306 | ||
307 | ||
308 | main() |