If I copy and paste your code straight into my editor and run it, I get an indentation error. Looks like something got screwed up in the post formatting:
$ python3 myage.py
File "myage.py", line 33
IndentationError: unindent does not match any outer indentation level
Aside from that, I was unable to find a malicious input that would trigger an unhandled exception in your code. 👍
No accounting for leap years
You compute the number of seconds somebody’s been alive as follows:
seconds = str(Age*31536000)
There’s no comment to explain where 31536000 came from. The program would be improved if you explained where this magic number came from, so that it can be verified independently (for example, when I first typed my previous sentence, I made a typo and missed a 0). Or even better, compute it on the fly. For example:
seconds = age * (60 * 60 * 24 * 365)
Now it’s much easier to see (1) where the quantity comes from, and (2) that it’s correct.
Your program doesn’t account for leap years correctly. For example, somebody who’s 20 years old has lived through the leap years 2016, 2012, 2008, 2004, (not 2000!) and maybe some of 1996. That’s up to 5 * 24 * 3600 = 432,000 seconds you’re missing.
In this case, that’s fairly small and perhaps insignificant – but it’s worth remembering leap years whenever you’re dealing with dates and times.
Read PEP 8
PEP 8 is the Python style guide, and you should read it and apply it to your code. Lots of weird stuff around spacing, line length, variable names, and so on, that make it harder to read your code. Standards make it easy for other experienced Python programmers to read your code.
If you need help, there are automated tools like flake8 which can analyse a Python file and give you suggestions for making it PEP 8 compliant.
Break up your code into functions
Nothing in this program is reusable, it’s just one long top-level execution. Breaking it up into smaller pieces will make those pieces easier to test, and easier to reuse. Some candidates for code that could be broken off into their own chunk:
- Get input from the user
- Given a validated input, how many seconds has somebody been alive
- Print the input
You get the idea. This tends to pay off in code quality and readability as well – small, self-contained functions are easier to work on than a monolith.
Use the standard library for pretty number printing
splitthousands() function is fine in places where commas are used to format numbers, but in other places people may used periods, or spaces, or not group digits at all. A more flexible solution is to use the
locale module from the standard library, and let it absorb that messiness for you:
print(locale.format('%d', seconds_alive, grouping=True))
Again, in this case that might not matter – but it’s something worth thinking about for bigger projects in the future.