0
Can someone check my code? Spot any errors or tell me how to make it more efficient
It's an age calculator that I made, I'm sure that there are better ways to do so but I just want to see if I made any mistakes or if I can improve in some areas. EDIT: some things to note: - I just copy/pasted the code into my code playground and saved it there. So just click my profile to see - I chose to just make the user type the month's number to save me work as this code was just a little challenge I set for myself
2 Réponses
+ 1
Instead of asking user to enter more lines of input, I would choose today date automatically by
import time
t =time.localtime()
print(t.tm_mday, t.tm_mon,t. tm_year)
And ask user to enter date in one single input and split it.
Good code..
For code improvements, use your feed post..
0
Thank you both!
Having the current time automatically there is very helpful for efficiency.
I updated my code to utilize that and also have only one input (using the yyyy/mm/dd). There is also way more that I will go through from your comments, but for now I decided to just go off from what I already know instead of just copying your own code.
Here is my 2nd iteration (I will probably make a 3rd and 4th and so on, once reading some more through your comments and also learning more things in python) :
# Age Calculator
import time
t = time.localtime()
a = input('enter birth date (yyyy/mm/dd): ')
birthYear = int(a[:4])
birthMonth = int(a[5:7])
birthDay = int(a[8:10])
x = f'you are {t.tm_year - birthYear} years old'
y = f'you are {t.tm_year - (birthYear + 1)} years old'
if birthMonth - t.tm_mon == 0:
if birthDay - t.tm_mday == 0:
print(x)
if birthDay - t.tm_mday < 0:
print(x)
if birthDay - t.tm_mday > 0:
print(y)
elif birthMonth - t.tm_mon < 0:
print(x)
else:
print(y)