*args and **kwargs

First of all, wtf?

Haha, I feel like I’m learning a new language ( I guess because I am). I don’t know if I went temporarily brain-dead because I was working on this stuff all day, but I got this problem:

“Define a function called myfunc that takes in an arbitrary number of arguments, and returns a list containing only those arguments that are even.

Remember, don’t run the function, simply provide the definition.

To give an idea what the function would look like when tested:

  1. myfunc(5,6,7,8)
  2. # Output: [6, 8]

Added note: this exercise requires that the function return a list. Print statements will not work here.”

…and it took me a solid 2 hours to figure it out. I finally googled the correct thing (I’m learning that’s a key skill in this game) and found this nice little website: https://www.w3resource.com/python-exercises/python-functions-exercise-10.php which led me to using this code:

def myfunc(*args):
enum = []
for n in args:
if n%2==0:
enum.append(n)
return enum

And it finally worked!!!!!

But HOLY CRAP I’m going to need to work on this. Functions are where I get kind of stuck, but according to the lessons I’m doing, this is really the ‘meat and potatoes’ of programming. Once I’m a little bit better at using functions, I’ll be able to solve a lot more problems.

 

Well, my brain is shot for tonight.

 

Here’s my next problem that I’ll work on tomorrow:

“Define a function called myfunc that takes in a string, and returns a matching string where every even letter is uppercase, and every odd letter is lowercase. Assume that the incoming string only contains letters, and don’t worry about numbers, spaces or punctuation. Note that the first letter of the output should be lowercase.

Remember, don’t run the function, simply provide the definition.

To give an idea what the function would look like when tested:

  1. myfunc(‘Anthropomorphism’)
  2. # Output: ‘aNtHrOpOmOrPhIsM’

Added note: this exercise requires that the function return a string. Print statements will not work here.”

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s