joke_library.py 1.09 KB
import time
import requests
from joke_types import *


def tell_joke(joke: Joke) -> None:
    print("Telling a " + joke["type"] + " joke...")
    if joke["type"] == 'knock-knock':
        for line in joke["setup"].split("\n"):
            print(line)
            time.sleep(1)
    else:
        print(joke["setup"])
        time.sleep(1)
    print(joke["punchline"])


def rate_joke(joke: Joke) -> int:
    numerical_rating: int = -1
    while 0 > numerical_rating or numerical_rating >= 5:
        rating = input("Rate that joke on a scale from 0-4: ")
        if rating.isnumeric():
            numerical_rating = int(rating)
    return numerical_rating


def get_one_joke(type: JokeType = "any") -> Joke:
    if type == "any":
        return requests.get(
            "http://labradoodle.caltech.edu:3005/jokes/random").json()
    else:
        return requests.get("http://labradoodle.caltech.edu:3005/jokes/" +
                            type + "/random").json()[0]


def get_n_jokes(n: int, type: JokeType = "any") -> list[Joke]:
    jokes: list[Joke] = []
    ...
    assert len(jokes) == n
    return jokes