Hi, I’m Shauna! I’m a 37 year old transgender woman from Ontario, Canada. I’m also a Linux enthusiast, and a Web Developer by trade. Huge Star Trek fan, huge Soulsborne fan, and all-around huge nerd.

  • 0 Posts
  • 12 Comments
Joined 1 year ago
cake
Cake day: June 15th, 2023

help-circle









  • It’s not at all necessary, but I find it makes much easier to read code if you instead only use if statements and just return early when you’re in a function. For example, you could check isalpha(letter) == true is true then check letter + key <= 90 do the letter += key; return letter; then since letter + key must be > 90 if it didn’t already return a value, then you can do the while statement and return letter without needing an if statement at all. Then the isalpha(letter) == false is also unncessary, just return letter at the end.

    Like this:

    char rotate(char letter, int key)
    {
      if (isalpha(letter)
      {
        if (letter  + key <= 90)
        {
            letter += key;
            return letter;
        }
    
        do the while loop here
      }
    
      return letter;
    }
    
    

  • This is a pretty compact and - I think - easy to read way of doing it:

    while(display != list(chosen_word)):

    guess = input("Guess a letter: ").lower()

    display = list(map(lambda c, d: c if d != '_' or c == guess else d, chosen_word, display))

    print(display)

    print("Congrats! You did it!")

    Mapping over an array is a pretty powerful tool and also using ternary expressions. If you’re not familiar, a map basically just iterates over an array and runs a function on that item, replacing it with whatever the return value of the function is.

    For example:

    ones = [1, 1] twos = list(map(lambda n: n + 1, ones))

    It’s running the lambda function with n as a parameter and returning n + 1, and it’s pulling the numbers from the array “ones”.

    Then ternary expressions I also find quite powerful. The format of which is basically:

    (result if true) if (condition to check) else (result of false)

    Or:

    2 if 1 + 1 == 2 else "You broke math. How did you do that?"