Dog age python задача

Напишите программу, которая вычисляет возраст собаки в человеческих годах

На вход программе подается число nn – количество собачьих лет. Напишите программу, которая вычисляет возраст собаки в человеческих годах.

Формат входных данных
На вход программе подаётся натуральное число – количество собачьих лет.

Формат выходных данных
Программа должна вывести возраст собаки в человеческих годах.

Примечание. В течение первых двух лет собачий год равен 10.5 человеческим годам. После этого каждый год собаки равен 4 человеческим годам.

x = int(input()) age = 10.5 age_2 = 4 if x  2: print(x*age) else: print(2*age + x*age_2)

Вопрос, почему при значении x = 3 всё равно считает по первой формуле?
по условию «if» x

Напишите программу, которая показывает возраст одноклассников в годах
Напишите программу которая показывает возраст одноклассников в годах

Напишите программу, которая преобразует ваш возраст в годах в количество дней
В общем в учебнике такое задание, а ответа нет.. до этого ничего такого в самом учебнике не было.

Составьте программу, которая запрашивает возраст школьника и вычисляет средний возраст школьников
Составьте программу, которая запрашивает возраст школьника и вычисляет средний возраст школьников.

else: print(2 * age + (x-2) * age_2)

Добавлено через 2 минуты
Учитесь пользоваться дебагером, по нему видно что при х = 3 идет не по первому условию, а считает неправильно.

Напишите программу, которая вычислит ваш возраст в часах
Напишите программу, которая вычислит ваш возраст в часах .Чтобы узнать текущую дату и время.

Напишите программу, которая вычисляет значение функции:
Напишите программу, которая вычисляет значение функции: y = a**2 + b**2, a >= 0, b >= 0; a**2 -.

Напишите программу, которая вычисляет значение выражения:
Напишите программу, которая вычисляет значение выражения: y = max(a,b) + max(b, 2c) Чего не.

Напишите программу, которая вычисляет функцию exp(x)
Напишите программу, которая вычисляет функцию exp(x) через сумму ряда для заданного пользователем.

Напишите программу, которая вычисляет значение функции:
Напишите программу, которая вычисляет значение функции: y = a**2 + b**2, a >= 0, b >= 0;.

Источник

Python Exercise: Calculate a dog’s age in dog’s years

Write a Python program to calculate a dog’s age in dog years.
Note: For the first two years, a dog year is equal to 10.5 human years. After that, each dog year equals 4 human years.

Pictorial Presentation:

Python Exercise: Calculate a dog

Sample Solution:

Python Code:

h_age = int(input("Input a dog's age in human years: ")) if h_age < 0: print("Age must be positive number.") exit() elif h_age  
Input a dog's age in human years: 12 The dog's age in dog's years is 61

Flowchart: Calculate a dog

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

Follow us on Facebook and Twitter for latest update.

Python: Tips of the Day

How to delete a file or folder?

  • os.remove() removes a file.
  • os.rmdir() removes an empty directory.
  • shutil.rmtree() deletes a directory and all its contents

Path objects from the Python 3.4+ pathlib module also expose these instance methods:

  • pathlib.Path.unlink() removes a file or symbolic link.
  • pathlib.Path.rmdir() removes an empty directory.
  • Weekly Trends
  • Java Basic Programming Exercises
  • SQL Subqueries
  • Adventureworks Database Exercises
  • C# Sharp Basic Exercises
  • SQL COUNT() with distinct
  • JavaScript String Exercises
  • JavaScript HTML Form Validation
  • Java Collection Exercises
  • SQL COUNT() function
  • SQL Inner Join
  • JavaScript functions Exercises
  • Python Tutorial
  • Python Array Exercises
  • SQL Cross Join
  • C# Sharp Array Exercises

We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

Источник

Homework 1b: Calculating How Old Your Dog is in Human Years Solution

Introduction to Python Programming
University of Pennsylvania

Homework 1b: Calculating How Old Your Dog is in Human Years

This assignment is designed to give you practice writing code and applying lessons and topics for the current module.

This homework deals with the following topics:

  • Getting user input
  • Error checking
  • Variables & data types
  • Conditionals

In this assignment, you will write a program that calculates a dog’s age in human years.

The program will prompt the user for an age in dog years and calculate that age in human years. Allow for int or float values, but check the user’s input to make sure it's valid -- it should be numeric and positive. Otherwise, let the user know their input is not valid.

You can use the following rules to approximately convert a medium-sized dog’s age to human years:

  • For the first year, one dog year is equal to 15 human years
  • For the first 2 years, each dog year is equal to 12 human years
  • For the first 3 years, each dog year is equal to 9.3 human years
  • For the first 4 years, each dog year is equal to 8 human years
  • For the first 5 years, each dog year is equal to 7.2 human years
  • After that, each dog year is equal to 7 human years. (Note: This means the first 5 dog years are equal to 36 human years (5 * 7.2) and the remaining dog years are equal to 7 human years each.)

Print the result in the following format, substituting for and : "The given dog age is in human years." Round the result to 2 decimal places. Note: If there is a 0 in the hundredths place, you can drop it, e.g. 24.00 can be displayed as 24.0.

  • If the user enters 2, the program will print: “The given dog age 2 is 24.0 in human years.”
  • If the user enters 3, the program will print: “The given dog age 3 is 27.9 in human years.”
  • If the user enters 4.5, the program will print: “The given dog age 4.5 is 32.4 in human years.”
  • If the user enters 12.1, the program will print: “The given dog age 12.1 is 85.7 in human years.”

Considering invalid inputs:

  • Your program must ask the user for an age in dog years - hint: use the input() function
  • We are going to test invalid inputs - make sure that your code can handle negative value inputs and non-numerical inputs!
  • For invalid inputs, make sure that your printed response adheres to the following:

- If a text-based input is provided, make sure your response contains the word ' invalid '. For example, if the user doesn’tinput a number, print “ is invalid.”, substituting for .

- If a negative input is provided, make sure your response contains the word ' negative '. For example, if the user inputs a negative number, print “Age cannot be a negative number.”

Источник

Dog age python задача

Star

Feb-26-2021, 04:04 PM (This post was last modified: Feb-26-2021, 04:15 PM by buran.)

Can anyone solve this problem?

“How Old is Your Dog in Human Years?” Calculator

Write a program that calculates a dog’s age in human years. The program will prompt the user for an age in dog years and calculate that age in human years. Allow for int or float values, but check the user’s input to make sure it's valid -- it should be numeric and positive. Otherwise, let the user know their input is not valid.

You can use the following rules to approximately convert a medium-sized dog’s age to human years:
For the first year, one dog year is equal to 15 human years
For the first 2 years, each dog year is equal to 12 human years
For the first 3 years, each dog year is equal to 9.3 human years
For the first 4 years, each dog year is equal to 8 human years
For the first 5 years, each dog year is equal to 7.2 human years

After that, each dog year is equal to 7 human years. (Note: This means the first 5 dog years are equal to 36 human years (5 * 7.2) and the remaining dog years are equal to 7 human years each.)
Print the result in the following format, substituting for dog_age and human_age: "The given dog age dog_age is human_age in human years." Round the result to 2 decimal places. Note: If there is a 0 in the hundredths place, you can drop it, e.g. 24.00 can be displayed as 24.0.

Considering invalid inputs:
Your program must ask the user for an age in dog years - hint: use the input() function
We are going to test invalid inputs - make sure that your code can handle negative value inputs and non-numerical inputs!

For invalid inputs, make sure that your printed response adheres to the following:
If a text-based input is provided, make sure your response contains the word 'invalid'.
If a negative input is provided, make sure your response contains the word 'negative'.

import traceback def calculator(): # Get dog age dog_age = input("Enter your dog's age ") try: # Cast to float dog_age = float(dog_age) except: print(dog_age, "is an invalid age.") print(traceback.format_exc()) calculator()

buran write Feb-26-2021, 04:15 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.

Источник

Читайте также:  Open html files in safari
Оцените статью