M
M
MirMirOrKirmasa2021-11-01 20:16:38
Python
MirMirOrKirmasa, 2021-11-01 20:16:38

What did I write wrong in the code?

import datetime
from datetime import datetime

FIO = input("Ваша Фамилия Имя и Отчество: \n")
print("Здравствуйте,",FIO)

AgePol = int(input("Введите свой возраст: \n"))
if AgePol < 0:
    AgePol = AgePol * -1
else:
    AgePol = AgePol

TY = datetime.now().year

AgePC = 1985
AgePC1 = TY - AgePC

Res = int(AgePC1 - AgePol)
Res_2 = int(Res * -1)

print("TY=",TY)
print("AgePC1=",AgePC1)
print("Res=",Res)
print("Res_2=",Res_2)

if Res>0:
    if AgePol == 22 or AgePol == 23 or AgePol == 24 or AgePol == 25:
        print('Вы старше меня на', Res, "лет.")
    else:
        if Res%10 == 0:
            print("Мы ровесники)))")
        elif Res%10 == 1:
            print('Вы младше меня на', Res,"год.")
        elif Res%10 >1<5:
            print('Вы младше меня на', Res,"года")
        elif Res%10 >=5<=20:
            print('Вы младше меня на', Res,'лет.')

elif Res < 0:
    if Res_2 == 0:
        print("Вы страше меня на",Res_2,"год.")
    elif 1 < Res_2%10 <= 4:
        print("Вы старше меня на", Res_2, "года.")
    elif 5 < Res_2%10 <= 20:
        print("Вы старше меня на", Res_2, "лет.")


My code, in theory, should display the difference between the age of the PC and the user. And everything seems to work, but if the number that we enter (AgePol) ends in 6 (the code ends stupidly) or in 1 in a three or more digit number (Just like with 6), then the output is incorrect. What did I write wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2021-11-01
@MirMirOrKirmasa

I would advise you not to try to figure it out, there is already some kind of mess, starting from multiplying by -1, ending with if blocks. Start over, name the variables so that it is clear what lies in them. It seems to me that you do not understand what Res is and what Res_2 is and why they are needed. The task is very simple, you need to subtract one number from another and that's it.

decision
user_age = int(input('Введите свой возраст: '))

current_year = datetime.now().year

pc_age = current_year - 1985

if user_age > pc_age:
    print('Вы старше на', user_age-pc_age, 'лет')
elif user_age == pc_age:
    print('Мы ровесники')
else:
    print('Вы младше на', pc_age-user_age, 'лет')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question