-1

I am trying to create a program that when a dogs age is inputted it will figure out the age in human years. If a negative number is inputted it should print that a negative number is not valid. Also if the input is not a number it should print that the input was not valid. my code runs in my editor however in the jupyter notebook it hangs on dog_age being undefined. I don't know if the variable "a" is going to also have the same issue.

dog_age = input("How old is your dog in years?")

try:
    d_a = float(dog_age)

    if d_a < 0:
        print(" Your input can not be negative! ")
    if (d_a >= 0) & (d_a <= 1):
        a = d_a * 15
    if d_a == 1:
        a = 15
    if (d_a > 1) & (d_a > 2):
        a = (d_a * 12)
    if d_a == 2:
        a = 24
    if (d_a > 2) & (d_a < 3):
        a = (d_a * 9.3)
    if d_a == 3:
        a = 27
    if (d_a > 3) & (d_a < 4):
        a = (d_a * 8)
    if d_a == 4:
        a = 32
    if (d_a > 4) & (d_a < 5):
        a = (d_a * 7.2)
    if d_a >= 5:
        a = (d_a * 7)
except ValueError as e:
    print("Your input was not valid")

round(a, 2)
a = str(a)

print("You inputted your dogs age as " + dog_age + " that is equal to " + a + " Human years old")
4
  • 1
    Welcome to Stack Overflow! Please always post the whole error message with full traceback. Commented Aug 10, 2021 at 5:09
  • You're not doing anything with the result of round(a, 2). It doesn't modify the variable in place, it returns the rounded value. So that should be a = round(a, 2) Commented Aug 10, 2021 at 5:10
  • I don't understand the complexity of this code. Isn't it just human years = 7 x dog years? Commented Aug 10, 2021 at 5:12
  • 1
    There's no reason why dog_age should be undefined after you respond to the input prompt. Commented Aug 10, 2021 at 5:12

1 Answer 1

0

If you enter an age between 1 and 2, a won't have any value:

if (d_a > 1) & (d_a > 2):
        a = (d_a * 12)

Besides if d_a == 1: is already covered by the previous statement.

Finally as pointed out by Sujay, the logical operator in Python is and. & is a bitwise operator which is unnecessary in your case.

Sign up to request clarification or add additional context in comments.

1 Comment

and and not &?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.