9/12/2020

Code expiation 2

 Hello!

This is the code explanation 2!

This is the question today.

Create a function that takes two parameters and, if both parameters are strings, add themas if they were integers or if the two parameters are integers, concatenate them.

From : Edabit/Stupid Addition 

This is my solved code.

def stupid_addition(a, b):
if type(a) !=type(b):
return None
if type(a)==int:
return str(a)+str(b)
else:
return int(a)+int(b)

This question is about some stupid addition.

It should add by the same type.

For example, how about adding "4" + "5"?

It's going to be 45, as it is adding by strings.

How about adding 4+ 5?

It's going to be 9, as it is adding by numbers(integers).

Let's think about the first if.

The first if is about when the type is added by a string and an integer.

Then, is will return None.

The second if is when we add with int, it will add by string.

It should return with a string.

The third one is else, that's going to be when we add with string, it is added by integer.

So, it should return with an integer.

So, Bye!!!






0 件のコメント:

Super Leo 4!

  Leo : Let's do this! Reimu : I'll kill those monsters, so you go! Leo : Yeah! I'll go to the goal!  Leo : Let's go〜! ? Wai...