Вопрос задан 23.06.2023 в 13:21. Предмет Информатика. Спрашивает Фискова Алина.

Посмотрите код в 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)
0 0
Перейти к ответам

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

Внимание! Ответы на вопросы дают живые люди. Они могут содержать ошибочную информацию, заблуждения, а также ответы могут быть сгенерированы нейросетями. Будьте внимательны. Если вы уверены, что ответ неверный, нажмите кнопку "Пожаловаться" под ответом.
Отвечает Насибов Эльсевар.

Ответ:

Попробуй:

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)

0 0
Отвечает нейросеть ChatGpt. Будьте внимательны, ответы нейросети могут содержать ошибочные или выдуманные данные.

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:

python
from 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:

  1. I added from datetime import datetime, date to import the necessary modules.
  2. I replaced the hard-coded birth year with an input that allows the user to enter their birth year.
  3. I corrected the datetime.strptime(input("2021")) to date(birth_year, 1, 1) to create a valid date of birth.
  4. The calculate_age function was modified to calculate the age correctly based on the birth date and the current date.
  5. The today object in the calculate_age function was created with date.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.

0 0

Похожие вопросы

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

Последние заданные вопросы в категории Информатика

Задать вопрос