I'm working on a UI and having trouble running my code through the second function. The initial function of the UI is supposed to take the client's full name and say ("Hello + full_name) and then take the client's date of birth and subtract it from the current date to calculate the client's age. The first part of this code goes through clearly I just do not know how to pass the code onto the second part of the code where the code prints out "Sorry we can not create your account at this time" or "Let's move forward with the next steps". Any help or advice would assist.
from datetime import datetime
full_name = input("Hi my name is Jacko. What is your full name?")
print ("Hello" + full_name)
class Jacko:
dob = input("Input your birthday, please: ")
date_of_birth = datetime.strptime(dob, "%Y %m %d")
current_date = datetime.today()
current_age = (current_date - date_of_birth) /365
def __init__(self, name):
self.name = name
def age (self):
self.current_age = current_age
if self.current_age < 21:
print ("Sorry we can not create your account at this time")
else:
print ("Let's move forward with the next steps")
There are many things not implemented properly in this example. For example, I don't see how you plan to use Jacko
class.
Secondly, the age is supposedly supplied by the user, so it should be in the __init__
and not be a class attribute. Class attributes are exactly the same for all instances of the class so unless you want all users to have the same age, age goes in the __init__
.
When calculating current_age
I assume we'd be interested in full years so use integer division instead of regular one. So I'd re-write the code like this:
from datetime import datetime
class Jacko:
def __init__(self, name, dob):
print(f'Hello {name}')
self.name = name
self.dob = datetime.strptime(dob, "%Y %m %d")
self.current_age_years = datetime.today().year - self.dob.year
def age (self):
if self.current_age_years < 21:
print ("Sorry we can not create your account at this time")
else:
print ("Let's move forward with the next steps")
if __name__ == '__main__':
full_name = input("Hi my name is Jacko. What is your full name? ")
dob = input('What is your date of birth?' )
jc = Jacko(full_name, dob)
jc.age()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.