This is the question below for everyone to have a better understanding:
Write a class definition for the Person class and write user-defined functions with these function headers:
def create_person(name, height, birthdate):
# Return a a new person object with the given name, height and birthdate.
# - name is a str
# - height is an int object in centimetres
# - birthdate is a date object from the
# module datetime
def get_age(person):
# Return the age of the person in years.
For example, assume today's date is June 12, 2018. if Mary was born on June 4, 2017, then Mary's age is 1. However, if Bob was born on June 14, 2018, then Bob would not have had a first birthday yet so the age is 0.
def get_description(person):
# Return a string object of the form: Name is
# N cm high and is M years old, where N and M
# are integers
For example, Michael is 190 cm high and is 43 years old or Samantha is 95 cm high and is 4 years old.
def main():
# Create a person named 'Michael', with height
# 190 cm, who was born on August 14, 1976 and
# output a description of this individual.
If you use a function from an imported module when writing your function, you usually declare the import statement at the top of your code.
Here is a sample run of a main program that just calls the main function.
Michael is 190 cm high and is 43 years old.
Hint:
Use the date class from the datetime module to represent a date. An object whose type is date, has attributes: year, month and day that you can use to compute the age of a Person.
To compute the current age of a person, you will need to first compute today's date. There is a method in the date class of the datetime module that creates a new date object that represents the current date. The name of this method is today. However, the special argument of this method must be the date class itself, instead of a particular object whose type is date. A method that is applied to a class object instead of to an instance of that class is called a class method.
Therefore, to create the current date you can use the expression:
date.today()
since after importing the date class from the datetime module, the identifier date is bound to the date class object.
To compute the age you can just subtract the year attribute of the birthdate from the year attribute of the current date. However, you will also need to check whether the person has already had their birthday yet this year and if not, subtract one year
This is the code I have so far, I'm getting an error
RuntimeErrorElement(RuntimeError,Error on line 27:
print(get_description(person)))
RuntimeErrorElement(RuntimeError,Error on line 22:
return person.name + ' is ' + str(person.height) + ' cm high and is ' + str(get_age(person)) + ' years old.'
AttributeError: type object 'person' has no attribute 'name'
The expected answer is suppose to be
Michael is 190 cm high and is 43 years old.
Can someone help me please?Thank you
from datetime import date
class person:
pass
def create_person(name, height, birthdate):
name = 'Michael'
height = 190
birthdate = date(1976, 8, 14)
return person
def get_age(person):
birthdate = person.birth
today = date.today()
subyear = 0
if today.month < birthdate.month or (today.month == birthdate.day and today.day <= birthdate.day):
subyear = 1
person.age = (today.year - (birthdate.year + subyear))
return person.age
def get_description(person):
return person.name + ' is ' + str(person.height) + ' cm high and is ' + str(get_age(person)) + ' years old.'
def main():
birthdate = date(1976, 8, 14)
person = create_person('Michael', 190, birthdate)
print(get_description(person))
You need to create a person object before setting the properties:
def create_person(name, height, birthdate):
p = person()
p.name = 'Michael'
p.height = 190
p.birthdate = date(1976, 8, 14)
return p
Even better, you should use the parameters:
def create_person(name, height, birthdate):
p = person()
p.name = name
p.height = height
p.birthdate = birthdate
return p
Related
I just learned what classes are today so I'm hoping this doesn't look too bad. 'model' refers to the model year.
class Car:
def __init__(self, model=0, price=0, year=0):
self.model = model
self.price = price
self.year = year
def depreciate(self, price):
rate = 0.15
age = da_car.model - da_car.year
for i in range(age):
value = price * rate
price = value
return value
def print_info(self):
print('Car\'s infortmation: \n Model year: ', da_car.model ,' \n Purchase price: ', da_car.price,' \n Current value: ', car_value)
da_car = Car()
da_car.model = int(input())
da_car.price = int(input())
da_car.year = int(input())
car_value = da_car.depreciate(da_car.price)
da_car.print_info()
I'm trying to return the depreciated value of the input price after x amount of years. Not sure why I'm getting this error. I tried making 'value' global but that did not work.
The issue is that the variable "value" actually is defined within the "for" loop, and that is its scope. So that variable and its information is not known outside of that loop. The program would most likely want/need a definition of that variable that would be visible within the class. With that in mind and testing out the code I made a few tweaks to the program. Following is a revised version of your program.
class Car:
def __init__(self, model=0, price=0.0, year=0):
self.model = model
self.price = price
self.year = year
self.value = price # This will be calculated later
def depreciate(self):
rate = 0.15
age = self.year - self.model # Correct age calculation
self.value = self.price # Initialize the value to the initial price
for i in range(age):
self.value = float(self.value - self.value * rate)
#price = value
return self.value
def print_info(self):
# The function within a method would not print data for a specific class (e.g. "da_car")
print('Car\'s infortmation: \n Model year: ', self.model ,' \n Purchase price: ', self.price,' \n Current value: ', self.value)
da_car = Car()
dx_car = Car()
print("First car statistics")
da_car.model = int(input("Model year: "))
da_car.price = float(input("Price: "))
da_car.year = int(input("Current year: "))
car_value = da_car.depreciate()
da_car.print_info()
print("Second car statistics")
dx_car.model = int(input("Model year: "))
dx_car.price = float(input("Price: "))
dx_car.year = int(input("Current year: "))
car_value = dx_car.depreciate()
dx_car.print_info()
Following are some key points.
In order to make the value variable pertinent to the depreciation function and to the car printing function, it was added as an attribute to the class.
In the various class functions, it would not be appropriate to use object specific variables in the calculations; rather class defined variables make more sense - so references to a specific object were changed to the "self" reference.
In testing I noticed the calculation for age was incorrect - the two variables needed to be reversed.
A second object definition was added to accentuate the point of usage of classes and objects and attempt to clarify why one would not want to use object specific variables in a class function.
With those revisions in place, a test run of the program was done.
#Dev:~/Python_Programs/Cars$ python3 Cars.py
First car statistics
Model year: 2020
Price: 32000
Current year: 2025
Car's infortmation:
Model year: 2020
Purchase price: 32000.0
Current value: 14198.57
Second car statistics
Model year: 2018
Price: 44000
Current year: 2025
Car's infortmation:
Model year: 2018
Purchase price: 44000.0
Current value: 14105.391884375002
Some print formatting would be beneficial for the current value but this should highlight the setup and use of objects based upon class definitions.
Give that a try and see if it meets the spirit of your project.
This is my code so far, the error I'm having so far is (name 'person' is not defined) on line person.name = name, I'm literally stuck & trying to figure out what is the issue/error since my code has to be based on the question I wrote below.I'm not sure if the remaining of my code has any errors since it is detecting that error first.
from datetime import date
class person:
pass
def create_person(name, height, birthdate):
person.name = name
person.height = height
get_age = birthdate
return person
def get_age(person):
birthdate = person.birth
today = date.today()
subyear = 0
if today.month < birthdate.month or (today.month == birthdate.day and today.day <= birthdate.day):
subyear = 1
person.age = (today.year - (birthdate.year + subyear))
return person.age
def get_description(person):
return person.name + ' is ' + str(person.height) + ' cm high and is ' + str(get_age(person)) + ' years old'
def main():
birthdate = date(1976, 8, 14)
person = create_person('Michael', 190, birthdate)
print(get_description(person))
This is the question:
Write a class definition for the Person class and write user-defined
functions with these function headers:
def create_person(name, height, birthdate):
# Return a a new person object with the given name, height and birthdate.
# - name is a str
# - height is an int object in centimetres
# - birthdate is a date object from the
# module datetime
def get_age(person):
# Return the age of the person in years.
For example, assume today's date is June 12, 2018. if Mary was born on
June 4, 2017, then Mary's age is 1. However, if Bob was born on June
14, 2018, then Bob would not have had a first birthday yet so the age
is 0.
def get_description(person):
# Return a string object of the form: Name is
# N cm high and is M years old, where N and M
# are integers
For example, Michael is 190 cm high and is 43 years old or Samantha is
95 cm high and is 4 years old.
def main():
# Create a person named 'Michael', with height
# 190 cm, who was born on August 14, 1976 and
# output a description of this individual.
If you use a function from an imported module when writing your
function, you usually declare the import statement at the top of your
code.
Here is a sample run of a main program that just calls the main
function.
Michael is 190 cm high and is 43 years old.
This is a hint I currently received:
Use the date class from the datetime module to represent a date. An
object whose type is date, has attributes: year, month and day that
you can use to compute the age of a Person.
To compute the current age of a person, you will need to first compute
today's date. There is a method in the date class of the datetime
module that creates a new date object that represents the current
date. The name of this method is today. However, the special argument
of this method must be the date class itself, instead of a particular
object whose type is date. A method that is applied to a class object
instead of to an instance of that class is called a class method.
Therefore, to create the current date you can use the expression:
date.today()
since after importing the date class from the datetime module, the
identifier date is bound to the date class object.
To compute the age you can just subtract the year attribute of the
birthdate from the year attribute of the current date. However, you
will also need to check whether the person has already had their
birthday yet this year and if not, subtract one year
Have Person create its own attributes
class Person:
def __init__(self, name, height, birthdate):
self.name = name
self.height = height
self.birthdate = birthdate
# why someone would write such a function is beyond me, but...
def create_person(name, height, birthdate):
return Person(name, height, birthdate)
# test
p = create_person('myname', 100, '23-23-23')
print(p.name)
This produces
myname
Now the other functions will have a class instance they can use.
If you have a problem with any one of the functions, that's best posted in another question that focuses just on that problem. (removing functions / methods not needed to demonstrate the problem).
I have created an employee base class
class Employee():
description = "A general employee"
def __init__(self, employee_id: int, name: str, surname: str, dob: str):
self.employee_id: int = employee_id
self.name = name
self.surname = surname
self.dob = parse(dob)
self._age
#property
def age(self):
today = date.today()
age = today.year - self.dob.year - ((today.month, today.day) < (self.dob.month, self.dob.day))
return age
I ran the unit test on the employee class the test code below it fails.
def employee():
return Employee(1001, 'John', 'Smith', '1975-08-25')
...
class TestEmployee(TestCase):
def test_age(self):
age = employee.age('1975-08-25')
assert age == 44
The results from test were:
> age = employee.age('1975-08-25')
E AttributeError: 'function' object has no attribute 'age'
Your #property age should not get any parameters. You are giving the value that you later expect in employee.dob as a parameter right now.
The corrected test would be
def test_age(self):
e = employee()
age = e.age
assert age == 44
However, this bears the issue that the age is calculated relative to now(), so this test will break at least once a year.
You can do one better by dynamically calculating the date of birth - this also enables you to test e.g. what the property would return a day before, after or exactly on the birthday:
from datetime import datetime, timedelta
def test_age(self):
yesterday = datetime.now() - timedelta(days=1)
employee = Employee(42, "Name" "Surname",'{year}-{month}-{day}'.format(year=yesterday.year - 44), month=yesterday.month, day=yesterday.day)
age = employee.age
assert age == 44
You simply cannot access year, month & day functions from string, you either need to pass date object or convert string date to date object.
emp_dob = datetime.datetime.strptime('1975-08-25', "%Y-%m-%d").date()
Then you can access emp_dob.year
Trying to figure out how to get the current date to lock in as a variable to subtract from the self.birthday that is input. I have looked at various examples and links to no avail...suggestions?
from datetime import datetime
import time
class Person(object):
def __init__(self, name):
self.name = name
self.birthday = None
#getName returns the name of the person
def getName(self):
return self.name
#setBirthday sets their Birthday to a date
def setBirthday(self):
day = raw_input('Please enter the date of the month you were born on here ->')
month = raw_input('Please enter the month of the year you were born on here ->')
year = raw_input('Please enter the year you were born on here ->')
self.birthday = int(day), int(month), int(year)
print self.birthday
#getAge returns how many days old the individual is
def getAge(self):
dateNow = datetime.datetime.now()
dateBirth = self.birthday
timedelta = dateNow - dateBirth
timedelta = self.daysOld
print self.daysOld
Error message is "dateNow = datetime.datetime.now()
AttributeError: type object 'datetime.datetime' has no attribute 'datetime'"
I have tried the following
datetime.date.today()
with no success
resources I'm using:
https://docs.python.org/3/library/datetime.html
https://pymotw.com/2/datetime/
datetime.datetime.now() refers to a method (now) of a class (2nd datetime) of a library (1st datetime), however you didn't import the entire library, just the single class (from datetime import datetime). Either import the entire library or call on the class without referring to the library.
Try
n= date.today()
I will give you the current date in string format then use split().
I am trying to create a class that contains the salary and bonus attributes and another that contains the name and idnum attributes. With a small program that asks if the shift has met goal for the year and then figures the total income for the shift supervisor for the year. Every time I try I get:
File "C:\Python33\12-2.py", line 53, in main
shift1 = Shiftsupervisor.Employee('28000.0','2240.0','Ian McGregor', 'S10001' )
AttributeError: type object 'Shiftsupervisor' has no attribute 'Employee'
What have I done wrong??
# This creates two classes - ShiftSupervisor & Employee
# The program then tells us what the annual income is
# This creates a class of Super Class Shiftsupervisor which contains salary, & bonus \
figures
class Shiftsupervisor:
#Initialize the Shiftsupervisor attributes
def __init__(self, salary, bonus):
self.__salary = salary
self.__bonus = bonus
# creates the mutator for the attributes
def set_salary(self, salary):
self.__salary = salary
def set_bonus(self, bonus):
self.__bonus = bonus
# returns the attributes
def get_salary(self):
return self.__salary
def get_bonus(self):
return self.__bonus
#Create the subclass of employee which holds the name & idnum
#Initialize the employee attributes
class Employee(Shiftsupervisor):
def __init__(self, salary, bonus, name, idnum):
Shiftsupervisor.__init__(self, salary, bonus)
#Initialize the employee new attributes
self.__name = name
self.__idnum = idnum
#creates the new mutator for name & id
def set_name(self, name):
self.__name = name
def set_idnum(self, idnum):
self.__idnum = idnum
# new method returns the name & id
def get_name(self):
return self.__name
def get_idnum(self):
return self.__idnum
#This program take info from the two classes and gives
# the total income for the Shift Supervisor
#Creates the shift supervisor objects
def main():
shift1 = Shiftsupervisor.Employee('28000.0','2240.0','Ian McGregor', 'S10001' )
shift2 = Shiftsupervisor.Employee('29500','2360.0','Brian Bory', 'S20202' )
shift3 = Shiftsupervisor.Employee('28750.0','2300.0''Finn McCool', 'S30045' )
def total_income():
if production == 'y' or 'Y':
return __salary + __bonus
else:
return __salary
#Ask the Question - Did they make production quota
production = input('Did Shift 1 make quota this year? Type Y for yes ' )
#Print the income
print(shift1.get_name(),'s Total income is: $', format(total_income, \
',.2f'), sep='')
#Ask the Question - Did they make production quota
production = input('Did Shift 2 make quota this year? Type Y for yes ' )
#Print the income
print(shift2.get_name(),'s Total income is: $', format(total_income, \
',.2f'), sep='')
#Ask the Question - Did they make production quota
production = input('Did Shift 3 make quota this year? Type Y for yes ' )
#Print the income
print(super3.get_name(),'s Total income is: $', format(total_income, \
',.2f'), sep='')
#call the main function
main()
Your code has the following problems:
I think it'd be better have ShiftSupervisor be a subclass of Employee. Unless I'm misunderstanding, a shift supervisor is a kind of employee, so employee is the base class. A shift supervisor might have additional attributes that specialize the Employee class. I've added the shift_number attribute to demonstrate this.
Your main method only creates the employees, but doesn't ever do anything with them.
Your total_income method is a bit confused. Remember, __salary and __bonus are attributes of an object. You have do always use the form instance.attribute in order to get access to those.
You don't need getters and setters in Python. The convention is to keep them normal fields that are meant to be publicly accessible, and use properties if it turns out you do need more complex accessing logic.
The salary and bonus shouldn't be a string -- they're numeric values.
Taken together, your new code might look something like this:
class Employee:
def __init__(self, salary, bonus, name, idnum):
self.salary = salary
self.bonus = bonus
self.name = name
self.idnum = idnum
class ShiftSupervisor(Employee):
def __init__(self, salary, bonus, name, idnum, shift_number):
super().__init__(salary, bonus, name, idnum)
self.shift_number = shift_number
def main():
shift1 = ShiftSupervisor(28000.0, 2240.0, 'Ian McGregor', 'S10001', 1)
shift2 = ShiftSupervisor(29500, 2360.0, 'Brian Bory', 'S20202', 2)
shift3 = ShiftSupervisor(28750.0, 2300.0, 'Finn McCool', 'S30045', 3)
find_income(shift1)
find_income(shift2)
find_income(shift3)
def find_income(employee):
production = input('Did shift {0} make quota this year? Type Y for yes '.format(employee.shift_number))
if production.lower() == 'y':
total_income = employee.salary + employee.bonus
else:
total_income = employee.salary
print("{0}'s Total income is: ${1}".format(employee.name, total_income))
main()
I'm also getting the feeling that you're entangling a shift and an employee in some way that you shouldn't be, though I'm quite able to put my finger on it. A shift might have more than one employee, and an employee could work multiple shifts, though this'll definitely depend based on what problem you're trying to solve.