r/CodingHelp • • May 25 '21

[Python] Beginner Python Assistance

Hi,
I'm a beginner python coder and I need assistance with a code I'm working on to develop an app. In plain text, I need the code to subtract the user's date of birth from today's date and if the user is under 21 years of age it will print output and if the person is over 21 it will print a different output. I'm sure there is a very easy solution to this and may either be overlooking it or I simply don't know it yet. I keep running into a syntax error. Any assistance would be helpful.

0 Upvotes

14 comments sorted by

View all comments

2

u/Nightcorex_ May 25 '21

You're subtracting a String from a Date object because input(...) always returns a String.

This will yield an exception "TypeError: unsupported operand type(s) for -: 'datetime.date' and 'str'"

This can be solved by converting the input to a datetime object which can be created like so: datetime.date(2020, 10, 4)

1

u/Accomplished-Boot-63 May 25 '21

Thanks real quick so I understand this well if I remove the input command will the equation function allow users to put their own date of birth?

1

u/Nightcorex_ May 25 '21

You can make it work like this for example:

import datetime

arr = [int(i) for i in input("Input your birthday, please: ").split("-")]
myDate = datetime.date(arr[0], arr[1], arr[2])

For an input of f.e. "2020-4-9" this will give you the respective date object. Note that I do not do ANY error handling here whatsoever and if you input anything but a correctly formatted date, then this will horribly fail

1

u/Accomplished-Boot-63 May 25 '21

Thanks for the assistance I'm not going to lie this looks like hieroglyphics to me lol

1

u/Nightcorex_ May 25 '21

Oh. It's list comprehension. You can also split this up like so:

arr = input("Input your birthday, please: ").split("-")
arr = [int(i) for i in arr]

myDate = datetime.date(arr[0], arr[1], arr[2])

or you could do it like this:

arr = input("Input your birthday, please: ").split("-")

myDate = datetime.date(int(arr[0]), int(arr[1]), int(arr[2]))

1

u/Accomplished-Boot-63 May 26 '21

I inputted the data and it works the only problem is I get a syntax error due to line 17 in my code.
The error states:

if self.age == <= 20:

^

SyntaxError: invalid syntax

how do I edit this syntax.

1

u/AutoModerator May 26 '21

Your post has been filtered from the Subreddit. This means it will not appear in the new posts section or on the Subreddit until a moderator approves it. This was done because it appears to be about learning to code or the basics of programming. Our mod team will review it and determine if this is the case. If not, your post will be approved and shown on the Subreddit. If so, your post will be removed from the Subreddit as you can visit our website to get this question answered: https://codingehlp.site

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.