7.6 4 Enthusiasm Codehs

7.6.4 Enthusiasm in CodeHS

In the 7.6.4 lesson in CodeHS, students are tasked with writing a function called add_enthusiasm that takes a string as input and returns the same string in all uppercase with an exclamation point added.

The solution is as follows:

def add_enthusiasm(string): return string.upper() + "!" 

This function works by first calling the upper() method on the input string to convert it to all uppercase. It then appends an exclamation point to the end of the string.

Here are some examples of how the function would be used:

>>> add_enthusiasm("hello") 
HELLO! 
>>> add_enthusiasm("i like dogs") 
I LIKE DOGS! 

Questions

Here are some questions that students might ask about the 7.6.4 lesson:

  • Why do we need to convert the string to all uppercase before adding an exclamation point?

The answer is that the exclamation point is a special character that can be interpreted differently depending on the case of the letters around it. For example, the string "hello!" is interpreted as a command in some programming languages. By converting the string to all uppercase, we ensure that the exclamation point is always interpreted as a punctuation mark.

  • What other ways could we write the add_enthusiasm() function?

One alternative way to write the function is as follows:

def add_enthusiasm(string): return f"{string.upper()}!" 

This version of the function uses the f-string formatting syntax to combine the uppercase version of the string with the exclamation point.

Another alternative is to use the str.casefold() method to convert the string to lowercase before adding an exclamation point. This would result in the following function:

def add_enthusiasm(string): return string.casefold() + "!".upper() 

This version of the function would capitalize the first letter of the string and add an exclamation point.

Conclusion

The 7.6.4 lesson in CodeHS is a basic introduction to functions in Python. It teaches students how to write a function that takes a string as input and returns a modified version of the string. The solution to the lesson is simple, but it provides a good foundation for more complex functions that students will encounter in later lessons.

Check Also

Apa arti dan makna dari kata Bravo?

Kata “bravo” adalah sebuah kata yang berasal dari bahasa Italia yang berarti “bagus” atau “hebat”. …

Leave a Reply

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