Вопрос задан 19.06.2023 в 23:37. Предмет Информатика. Спрашивает Андрієшин Олег.

Python. Считается, что один год, прожитый собакой, эквивалентен семи человеческим годам. При этом

зачастую не учитывается, что собаки становятся абсолютно взрослыми уже к двум годам. Таким образом, многие предпочитают каждый из первых двух лет жизни собаки приравнивать к 10,5 года человеческой жизни, а все последующие – к четырем. Напишите программу, которая будет переводить человеческий возраст в собачий с учетом указанной выше логики. Убедитесь, что программа корректно работает при пересчете возраста собаки меньше и больше двух лет. Также программа должна выводить сообщение об ошибке, если пользователь ввел отрицательное число.
0 0
Перейти к ответам

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

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

Ответ:

a = int(input())

if a <= 0:

   print('Введено неверное число')

elif a == 1:

   print(10.5)

elif a == 2:

   print(30)

else:

   print(30+((a-2)*4))

Объяснение:


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

To convert a human age to a dog age, taking into account the logic mentioned above, you can use the following program in Python:

```python def convert_to_dog_age(human_age): if human_age < 0: raise ValueError("Human age cannot be negative") elif human_age <= 2: dog_age = human_age * 10.5 else: dog_age = 21 + (human_age - 2) * 4 return dog_age

try: human_age = int(input("Enter the human age: ")) dog_age = convert_to_dog_age(human_age) print("The dog age is:", dog_age) except ValueError as e: print("Error:", str(e)) ```

This program defines a function `convert_to_dog_age` that takes a human age as input and returns the corresponding dog age. The function uses an if-else statement to apply the conversion logic. If the human age is less than or equal to 2, it multiplies the age by 10.5. Otherwise, it adds 21 to the result of multiplying the difference between the human age and 2 by 4.

The program then prompts the user to enter a human age, converts it to a dog age using the `convert_to_dog_age` function, and prints the result. If the user enters a negative number, a `ValueError` is raised and an error message is displayed.

Please note that this program assumes the user will enter a valid integer for the human age. If you want to handle invalid inputs, you can modify the program to include appropriate error handling.

I hope this helps! Let me know if you have any further questions.

0 0

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

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

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

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