[Tutor] Python Error

Alan Gauld alan.gauld at yahoo.co.uk
Thu May 27 09:05:14 EDT 2021


On 27/05/2021 02:16, Patrik Fougere wrote:

> coder. I'm running into a couple of issues and I would love any input on
> how to fix it. The first issue was turning string data into datetime data
> (I found an answer on google). The next issue was a value error in regards
> to the input of the string data. I've attached the image to this email. 

Its much easier to respond to if you just copy n paste the code into the
email - as I've done below...

> 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

The 4 lines above should be inside the __init__() method, otherwise
you only get one person's birthday for the class not one per person..

Everything unique to an instance should be inside __init__()

>     def __init__(self, name):
>        self.name = name

>     def age (self):

This should probably be called check_age() or similar since age()
suggests that it returns the age. Its just a bit confusing.


>         self.age = self.current_age/365

And here we have a problem because you overwrite your method with data.
self.age is the method you are calling. By doing this you lose the method.

>         if self.age < 21:
>             print ("Sorry we can not create your account at this time")
>         else:
>             print ("Let's move forward with the next steps")

If you are getting a ValueError please include the full error text.
It contains lots of useful data.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list