-3

Is there anything better that can reduce my code ?

a=int(input('birth year'))
present_year=2020
while a<1900 or a>2020:
    print('U are funny, arent you ?  ')
    a=int(input('ENTER AGAIN PLEASE, your birth year: '))
while a>=1900 and a<2020:
    print('you are ',(present_year-a),'years old')
    a=int(input('NEXT ONE : '))
    while a<1900 or a>2020:
        print('Error ,')
        a=int(input('RE-ENTER YOUR BIRTH YEAR'))
  • Yes. You could look up [https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) for some inspiration. Then clean up your code, use function for the input validation and read up on `while True: ..... break` syntax to escape the loop. Teaching programming is not really SO's fortè so maybe do some tutorials. – Patrick Artner Apr 12 '20 at 10:28

1 Answers1

0

I reckon this should be better

presentYear = int(2020)
while True:
    a = int(input('enter your birth year\n'))
    if (a>1900 and a<2020):
        age = presentYear-a
        print("Your age is "+ str(age) )
    else:
        print('error')