generated from mwc/lab_riddles
1. One service I use regularly is spotify to listen to music. One feature is creating playlists with different amount of songs in them. One thing behind the scenes with HTTP requests could be with trying to add a song that already exists in a playlist, this would result in some message saying the song is already in the playlist. If the song is not in the playlist it gets added. 2. I will definitly say that I was thinking too much about the lab and ended up trying complicated things but in reality it was a simple fix. This makes me think more about my tech interactions as simpler than I would have expected. However with the errors I recieved it shows how much more there is behind that I didn't see. I was able to see what actually goes into coding an application.
92 lines
3.0 KiB
Python
Executable File
92 lines
3.0 KiB
Python
Executable File
# RiddleView
|
|
# ------------------
|
|
# By Chris Proctor
|
|
# RiddleView provides a nice user interface for guessing riddles.
|
|
|
|
|
|
from api import RiddleAPI
|
|
|
|
class RiddleView:
|
|
"Allows a player to interact with a Riddle Server from the Terminal"
|
|
def __init__(self, url):
|
|
self.api = RiddleAPI(url)
|
|
|
|
def run(self):
|
|
print("Welcome to the Riddler")
|
|
print("Press control + c to quit")
|
|
print("-" * 80)
|
|
self.show_menu()
|
|
|
|
def show_menu(self):
|
|
choices = [
|
|
"Show riddles",
|
|
"Random riddle",
|
|
"Add a riddle"
|
|
]
|
|
choice = self.get_choice("What do you want to do?", choices)
|
|
if choice == 0:
|
|
self.list_riddles()
|
|
elif choice == 1:
|
|
riddle = self.api.get_random_riddle()
|
|
self.ask_riddle(riddle['id'])
|
|
elif choice == 2:
|
|
self.add_riddle()
|
|
|
|
def list_riddles(self):
|
|
riddles = self.api.get_all_riddles()
|
|
if len(riddles) == 0:
|
|
print("Sorry, there are no riddles on the server!")
|
|
self.show_menu()
|
|
else:
|
|
choices = [riddle['question'] for riddle in riddles]
|
|
choice = self.get_choice("Which riddle do you want to guess?", choices)
|
|
riddle_id = riddles[choice]['id']
|
|
self.ask_riddle(riddle_id)
|
|
|
|
def ask_riddle(self, riddle_id):
|
|
riddle = self.api.get_riddle(riddle_id)
|
|
print(riddle['question'])
|
|
guess = input("> ")
|
|
response = self.api.guess_riddle(riddle_id, guess)
|
|
if response['correct']:
|
|
print("Yes!")
|
|
else:
|
|
print("Nope, that's not the answer.")
|
|
self.show_menu()
|
|
|
|
def add_riddle(self):
|
|
question = input("question: ")
|
|
answer = input("answer: ")
|
|
choices = ["Add it!", "Edit", "Never mind"]
|
|
choice = self.get_choice(question + " " + answer, choices)
|
|
if choice == 0:
|
|
self.api.add_riddle(question, answer)
|
|
self.show_menu()
|
|
elif choice == 1:
|
|
self.add_riddle()
|
|
elif choice == 2:
|
|
self.show_menu()
|
|
|
|
def get_choice(self, prompt, choices):
|
|
print(prompt)
|
|
for i, choice in enumerate(choices):
|
|
print("{}. {}".format(i, choice))
|
|
while True:
|
|
selection = input("> ")
|
|
if selection.isdigit():
|
|
number = int(selection)
|
|
if number >= 0 and number < len(choices):
|
|
return number
|
|
print("Please try again")
|
|
|
|
if __name__ == '__main__':
|
|
# Here we're adding an argument, so the user can optionally provide a different
|
|
# riddle server URL. ArgumentParser handles this option and also provides a nice help
|
|
# message. Try it out by running `python view.py --help`.
|
|
from argparse import ArgumentParser
|
|
parser = ArgumentParser("Riddle Client")
|
|
parser.add_argument("--server", default="https://riddles.makingwithcode.org", help="The URL of the Riddle Server")
|
|
args = parser.parse_args()
|
|
view = RiddleView(args.server)
|
|
view.run()
|