9/06/2020

Code explanation part 1

 Hello!

Today, I'll show you the code I solved code.

This is the question.


Create the instance attributes fullname and email in the Employee class. Given a person's first and last names:

  • Form the fullname by simply joining the first and last name together, separated by a space.
  • Form the email by joining the first and last name together with a . in between, and follow it with @company.com at the end. Make sure the entire email is in lowercase.

Question from = Edabit : Fullname and Email 


My solved code

from test import Test
#https://edabit.com/challenge/gB7nt6WzZy8TymCah
class Employee:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname
self.fullname = firstname + ' ' + lastname
self.email = '{}.{}@company.com'.format(firstname, lastname).lower()


This code is easy.
As you see, there is a class called "Employee".
In this class, there is the info about that employee.
If you want to know about classes, here is the link.
There is four info - first name, last name, full name and email.
The question prepared the classes and the info - first name and last name.
We need to fill up the code!
First, let's think about the full name.
Full name are made by the first name and the last name.
So, we will just plus the first name and the last name!
BUT!!!
There is no spaces!!!
We need to plus the space in the middle of the first name and last name.
Easy peasy lemon squeezy! 🍋
Next is going to be the email.
Emails are also easy.
Let's think.
In the question, it was written that the first name and last name's middle should be the "." and following by "@company.com'
So, we can put the first name, the last name, and the "@company.com" and the "."
Like this.
self.email = self.firstname + '.' + self.lastname + '@company.com'
We can do that but there is more easy way.
We can format it.
This is the link about the format method.
That's more easy!
Easy peasy lemon squeezy!🍋(Second time)




Test code


emp_1 = Employee("John", "Smith")
emp_2 = Employee("Mary", "Sue")
emp_3 = Employee("Antony", "Walker")
emp_4 = Employee("Joshua", "Senoron")
Test.assert_equals(emp_1.firstname, "John")
Test.assert_equals(emp_1.lastname, "Smith")
Test.assert_equals(emp_1.fullname, "John Smith")
Test.assert_equals(emp_1.email, "john.smith@company.com")
Test.assert_equals(emp_2.firstname, "Mary")
Test.assert_equals(emp_2.lastname, "Sue")
Test.assert_equals(emp_2.fullname, "Mary Sue")
Test.assert_equals(emp_2.email, "mary.sue@company.com")
Test.assert_equals(emp_3.firstname, "Antony")
Test.assert_equals(emp_3.lastname, "Walker")
Test.assert_equals(emp_3.fullname, "Antony Walker")
Test.assert_equals(emp_3.email, "antony.walker@company.com")
Test.assert_equals(emp_4.firstname, "Joshua")
Test.assert_equals(emp_4.lastname, "Senoron")
Test.assert_equals(emp_4.fullname, "Joshua Senoron")
Test.assert_equals(emp_4.email, "joshua.senoron@company.com")

If we try with this test, it should success.
So, that's the explanation.
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...