+ 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
3 Answers
+ 1
This is a nice code :))
The first thing I'd suggest is to write a function to make it look cleaner :))
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.
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