
Посмотрите код в Phyton, не могу вывести возраст, ответ должен быть 76 лет print("Enter your
name:") name = "JOHN" print(name.capitalize()) print("Enter your last name:") lastname = "Doe" print(lastname) print("When were you born?:") print("1945") date_of_birth = datetime.strptime(input("2021")) def calculate_age(born): today = date.today(2021) return today.year - born.year age = calculate_age(date_of_birth) print(age) print("Where are you from?") state = "canada" print(state.capitalize()) message = "Hello, %s %s . You are %s years old. You are living in %s" % (name, lastname, age, state) print(message)

Ответы на вопрос

Ответ:
Попробуй:
print("Enter your name:")
name = "JOHN"
print(name.capitalize())
print("Enter your last name:")
lastname = "Doe"
print(lastname)
print("When were you born?:")
print("1945")
date_of_birth = 1945
def calculate_age(born):
return 2021 - born
age = calculate_age(date_of_birth)
print(age)
print("Where are you from?")
state = "canada"
print(state.capitalize())
message = "Hello, %s %s . You are %s years old. You are living in %s" % (name, lastname, age, state)
print(message)



Looks like there are a few issues in your code. Here's the corrected code that will display the age as "76 years old" as you expect:
pythonfrom datetime import datetime, date
print("Enter your name:")
name = "JOHN"
print(name.capitalize())
print("Enter your last name:")
lastname = "Doe"
print(lastname)
print("When were you born?:")
birth_year = int(input("Enter your birth year: "))
date_of_birth = date(birth_year, 1, 1)
def calculate_age(born):
today = date.today()
age = today.year - born.year
if today.month < born.month or (today.month == born.month and today.day < born.day):
age -= 1
return age
age = calculate_age(date_of_birth)
print(age)
print("Where are you from?")
state = "canada"
print(state.capitalize())
message = "Hello, %s %s. You are %d years old. You are living in %s" % (name, lastname, age, state)
print(message)
In this corrected code:
- I added
from datetime import datetime, date
to import the necessary modules. - I replaced the hard-coded birth year with an input that allows the user to enter their birth year.
- I corrected the
datetime.strptime(input("2021"))
todate(birth_year, 1, 1)
to create a valid date of birth. - The
calculate_age
function was modified to calculate the age correctly based on the birth date and the current date. - The
today
object in thecalculate_age
function was created withdate.today()
to get the current date.
With these changes, the code should work as expected and display the age as "76 years old" based on the birth year entered by the user.


Похожие вопросы
Топ вопросов за вчера в категории Информатика







Последние заданные вопросы в категории Информатика
-
Математика
-
Литература
-
Алгебра
-
Русский язык
-
Геометрия
-
Английский язык
-
Химия
-
Физика
-
Биология
-
Другие предметы
-
История
-
Обществознание
-
Окружающий мир
-
География
-
Українська мова
-
Информатика
-
Українська література
-
Қазақ тiлi
-
Экономика
-
Музыка
-
Право
-
Беларуская мова
-
Французский язык
-
Немецкий язык
-
МХК
-
ОБЖ
-
Психология
-
Физкультура и спорт
-
Астрономия
-
Кыргыз тили
-
Оʻzbek tili