Get an integer

def getInt(prompt):
    while True:
        try:
            return int(input(prompt))
        except ValueError:
            pass

Get a string

def getStr(prompt):
    while True:
        try:
            input = int(input(prompt))
        except ValueError:
            return input
def getStr(prompt):
    while True:
        input = input(prompt)
        if not any(char.isdigit() for char in input):
            return user_input

Yes or No

def Yay_or_Nay():
    while True:
        YES = ["y", "y","Yes", "yes"]
        NO = ["N", "n", "No", "no"]
        x = str(input("Yes or no: "))
        if x in YES:
            return "Yes"
        elif x in NO:
            return "No"
        else:
            print("Please type yes or no")
            continue

Windows Alert

def alert(title, message, duration=10):

    # Show a system notification
    notification.notify(
        title=title,
        message=message,
        timeout=duration
    )

    # Sound an alert
    x = 10
    while x > 0:
        winsound.Beep(1000, 100)  # Beep at 1000 Hz for 100 ms
        time.sleep(.1)
        x -=1

Dark Mode

javascript:(function() {
    function toggleDarkMode() {
        var css = 'html { filter: invert(100%) hue-rotate(180deg); } img, video { filter: invert(100%) hue-rotate(180deg); }';
        var head = document.head || document.getElementsByTagName('head')[0];
        var style = document.createElement('style');
        style.type = 'text/css';
        style.id = 'dark-mode-style';
        
        if (style.styleSheet) {
            style.styleSheet.cssText = css;
        } else {
            style.appendChild(document.createTextNode(css));
        }

        if (document.getElementById('dark-mode-style')) {
            document.getElementById('dark-mode-style').remove();
            sessionStorage.setItem('darkMode', 'off');
        } else {
            head.appendChild(style);
            sessionStorage.setItem('darkMode', 'on');
        }
    }

    function applyDarkModePreference() {
        if (sessionStorage.getItem('darkMode') === 'on') {
            var css = 'html { filter: invert(100%) hue-rotate(180deg); } img, video { filter: invert(100%) hue-rotate(180deg); }';
            var head = document.head || document.getElementsByTagName('head')[0];
            var style = document.createElement('style');
            style.type = 'text/css';
            style.id = 'dark-mode-style';

            if (style.styleSheet) {
                style.styleSheet.cssText = css;
            } else {
                style.appendChild(document.createTextNode(css));
            }
            
            head.appendChild(style);
        }
    }

    applyDarkModePreference();

    var button = document.createElement('button');
    button.innerHTML = 'Toggle Dark Mode';
    button.onclick = toggleDarkMode;
    document.body.appendChild(button);
})();

Categories: functions

1 Comment

Andrew · 12 December 2023 at 16:55

A simple and useful function and a few cool bits of code. Found interesting. Followed!

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *