Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Dog Age Calculator (by AdyaTech)/dogage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Request input from the user to provide a dog's age in human years and convert it to an integer
h_age = int(input("Input a dog's age in human years: "))

# Check if the entered age is less than zero; if true, display an error message and exit the program
if h_age < 0:
print("Age must be a positive number.")
exit()
# If the entered age is 2 years or less, calculate the dog's age in dog's years using the formula 10.5 times the human years
elif h_age <= 2:
d_age = h_age * 10.5
# For ages greater than 2, calculate the dog's age in dog's years using the formula 21 plus 4 times the remaining human years after 2
else:
d_age = 21 + (h_age - 2) * 4

# Display the calculated dog's age in dog's years
print("The dog's age in dog's years is", d_age)
Comment on lines +1 to +16
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Don't add too many comments; code should be self-documenting
  • Check for valid input
  • If the script fails due to unexpected behaviours, then it should exit with a non-zero code.
  • The ternary operator is just a preference
Suggested change
# Request input from the user to provide a dog's age in human years and convert it to an integer
h_age = int(input("Input a dog's age in human years: "))
# Check if the entered age is less than zero; if true, display an error message and exit the program
if h_age < 0:
print("Age must be a positive number.")
exit()
# If the entered age is 2 years or less, calculate the dog's age in dog's years using the formula 10.5 times the human years
elif h_age <= 2:
d_age = h_age * 10.5
# For ages greater than 2, calculate the dog's age in dog's years using the formula 21 plus 4 times the remaining human years after 2
else:
d_age = 21 + (h_age - 2) * 4
# Display the calculated dog's age in dog's years
print("The dog's age in dog's years is", d_age)
try:
human_years = float(input("Input a dog's age in human years: "))
except ValueError:
print("Please enter a valid number")
exit(1)
if human_years < 0:
print("Age must be a positive number.")
exit(1)
dog_age = human_years * 10.5 if human_years <= 2 else 21 + (human_years - 2) * 4
print("The dog's age in dog's years is", dog_age)

Loading