Age Calculator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Age Calculator

I’ve learnt Python for like 5 minutes so I barely know anything but with the little I do I attempted to write something to calculate someone’s age and I know this can obviously be done neater and more efficiently so I was just wondering if anyone would like to show me how https://code.sololearn.com/c7bSK1h8ODkn/?ref=app https://code.sololearn.com/c7bSK1h8ODkn/?ref=app

6th May 2020, 8:28 PM
Rhys Harvey
Rhys Harvey - avatar
3 Answers
+ 1
This is a nice code :)) The first thing I'd suggest is to write a function to make it look cleaner :))
6th May 2020, 10:24 PM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
0
Hi Rhys. Thats a great job for a beginner, although I would recommend spacing the last chunk of code out a little so as its easier to read. Great job nevertheless! :D. Thanks.
6th May 2020, 9:42 PM
Codeize
Codeize - avatar
0
Looks good for a beginner! however you’ve got some array out of bounds exceptions there. you reference the arrays “months_previous” and “months_to_come” up to an 11th index whereas these arrays are sized less than 11. For example your “months_previous” array has only 4 strings contained within it, so calling months_previous[4] and higher will result in an out of bounds exception. second, you can significantly reduce the size of your program by using loops, doing so can also mitigate your out of bounds error by telling the loop to only iterate as many times as there is data available, like so: for month in months_previous: if month_birth == month: print("\nYou are " + str(calcAge) + " years old " + name + "!") that alone would remove 11 of your elif statements :) I actually made an age calculator in python a while ago, feel free to take a look at it! https://code.sololearn.com/cwZe6m7Lp5Nx/?ref=app
6th May 2020, 11:06 PM
Jake
Jake - avatar