Make your own Spongebob Font generator! “Dont use that weird spongebob mocking Me: Dont use thAt Welrd Spongebob meme” MockinG MEme This is an optional, extra credit exercise for you to have fun with strings, conditionals, and while loops. You will need to submit this function in a separate file labo3_bonus.py to a separate Gradescope assignment Lab03: Bonus This function is based off of the Mocking Spongebob Meme.Your task: to write a function that takes in a string message and returns the same message but so that it looks like the font from a Mocking Spongebob meme, meaning that it’s letters are randomly uppercase or lowercase. But in Python, strings are immutable (i.e you cannot directly modify them by changing individual characters). For example: >>> word = “hello” >>> word[ ] = “” Traceback (most recent call last): File “”, line 1, in cmodules word[e].” TypeError: ‘str’ object does not support item assignment Notice when we try to assign word(O) to be “”, we get an error. Because we cannot directly change/reassign the characters in the string message, we will instead declare an empty string variable called SpongebobText that we will add characters to one by one. We will access each character in message, decide whether to make a letter lowercase or uppercase, and then add it to SpongebobText.
To decide whether or not to make a letter uppercase or lowercase, we will pick a random number between 0 and 10 (you’ll see how to do that below). If that number is even, make the letter lowercase and add it to SpongebobText. Otherwise, make the letter uppercase and add it to SpongebobText. This function will require you to use Python functions you are probably unfamiliar with: two of them are someStr. lower() and soneStr.upper(), where someStr is a string variable, lower() takes a string variable and returns a lowercase version of it. upper takes a string variable and returns an uppercase version of it. >>> word “Hello” >>> word [@]. lower() “h >>> print (word) “Hello! >>> word. upper() “HELLO >> print (word) “Hello Note that upper() or lower() do not modify the variable word: they return a new string with the characters that are either in lower or upper case..
You will also have to use random. randint(a,b) which returns a random integer N such that a <>>> random.randint(0, 5) >>> random.randint(0, 7) For the spongebobFont() function, every time you index a character in message, you will want to generate a random integer between 0 and 10. Then, if that integer is even, you should add a lowercase version of that letter to SpongebobText. Otherwise, add an uppercase version of that letter to SpongeboText. Here is the starter code: import random def spongebobFont (message): – Given an input string, go through the string character by character and decide whether or not to make a specific character uppercase or lower case. -E.9.,
if message = “hello”, first look at the first character ‘h’. Then pick a random integer. If that integer is even, add a lowercase ‘h’ to SpongebobText. Otherwise, add an uppercase ‘H’ to SpongebobText. Do this for every character in message. SpongebobText = ” random.seed ( 7919) # This is the string we will add characters to and return at the end of our function # Do not edit this line! i = 0 # This is the index variable we will use to index message while i < …=”” #=”” todo:=”” while=”” i=”” is=”” less=”” that=”” the=”” length=”” of=”” sentence;=”” replace=”” …=”” with=”” the=”” rest=”” of=”” the=”” expre=”” num=”…” #=”” todo:=”” pick=”” a=”” random=”” number=”” from=”” @=”” to=”” 10=”” if=”” …=”” #=”” todo:=”” if=”” num=”” is=”” even;=”” replace=”” …=”” with=”” the=”” expression=”” to=”” check=”” if=”” num=”” is=”” even=”” spongebobtext=”” +=”…” #todo:=””>
make the letter at index i in message lowercase and add it to Sponge else: # otherwise num is odd SpongebobText += … # TODO: make the letter at index i in message uppercase and add it to Sponge i … # TODO: update i by 1 return Spongebob Text def test_spongebobFont(): assert spongebobFont(“Beep boop, I’m a computer”) == “DEEP boop, i’m a computer” Submit this function in a separate file lab03_bonus.py to a separate Gradescope assignment Labo3
The Correct Answer and Explanation is :
Here’s the implementation of the spongebobFont() function as described:
Explanation:
- Initialization:
- The function begins by creating an empty string,
SpongebobText, to build the modified version of the input string. random.seed(7919)is used to ensure reproducible results during testing.
- Iteration:
- A
whileloop is used to traverse the string character by character using the indexi. - At each iteration, a random number between 0 and 10 is generated.
- Decision Making:
- If the random number is even (
num % 2 == 0), the corresponding character inmessageis converted to lowercase and added toSpongebobText. - If the random number is odd, the character is converted to uppercase and added to
SpongebobText.
- Advancing:
- The index
iis incremented to move to the next character in the input string.
- Return:
- Once the entire input string is processed, the resulting
SpongebobTextis returned.
Key Python Features Used:
- Random number generation:
random.randint(0, 10)is utilized for randomness. - String immutability: Characters are added to a new string, rather than modifying the input string.
- String methods:
.lower()and.upper()are used to toggle case.
This implementation is efficient, adheres to the requirements, and ensures correct behavior with the given test case.