python-3.x 把狗的年龄换算成人类的年龄

6yoyoihd  于 4个月前  发布在  Python
关注(0)|答案(6)|浏览(83)

我已经很长时间没有编程了,我正在做一个将狗的年龄转换为人类年龄的函数,但是我遇到了一些错误,我无法通过它们。这是我的代码

def calculator():

    # Get dog age
    age = input("Input dog years: ")

    try:
        # Cast to float
        d_age = float(age)
        # If user enters negative number, print message
        if(d_age < 0):
            print("Age can not be a negative number", age)
        # Otherwise, calculate dog's age in human years
        elif(d_age == 1):
            d_age = 15
        elif(d_age == 2):
            d_age = 2 * 12
        elif(d_age == 3):
            d_age = 3 * 9.3
        elif(d_age == 4):
            d_age = 4 * 8
        elif(d_age == 5):
            d_age = 5 * 7.2
        else:
            d_age = 5 * 7.2 + (float(age) - 5) * 7
        print("\n \t \'The given dog age", age, "is", d_age, "human years.'")
    except ValueError:
            print(age, "is an invalid age")

calculator()

字符串
我不明白为什么d_age < 0不工作,我不明白为什么它会显示为错误“calculator()is not defined”。提前感谢!
注意:我看到已经有关于这一点的问题,但我正在以不同的方式做。
编辑:我只是没有用字典,因为它还没有在课程中介绍。我只是试着用我学到的东西。无论如何,谢谢。

f8rj6qna

f8rj6qna1#

看起来你的代码结构不正确。如果你移动elif块并做一些小的修改,它会工作。如下所示:

def calculator():

    # Get dog age
    age = input("Input dog years: ")

    try:
        # Cast to float
        d_age = float(age)
        # If user enters negative number, print message
        if(d_age < 0):
            print("Age can not be a negative number", age)
        # Otherwise, calculate dog's age in human years
        elif(d_age == 1):
            d_age = 15
        elif(d_age == 2):
            d_age == 2 * 12
        elif(d_age == 3):
            d_age == 3 * 9.3
        elif(d_age == 4):
            d_age = 4 * 8
        elif(d_age == 5):
            d_age = 3 * 7.2
        else:
            d_age = 5 * 7.2 + (age - 5) * 7
        print("\n \t \'The given dog age", age, "is", d_age, "human years.")
    except ValueError:
            print(age, "is an invalid age")

calculator()

字符串

vkc1a9a2

vkc1a9a22#

这是我的建议,尽管答案被接受了。

def calculator():
    age = input("Input dog years: ")
    try:
        d_age = float(age)
        ageDict = {1: 15, 2: 12, 3: 9.3, 4: 8, 5: 7.2}
        if d_age in ageDict:
            print(round(d_age*ageDict[d_age],2))
        else:
            print(round(5*7.2+(d_age - 5)*7,2))
    except ValueError:
        print(age,"is an invalid age")

calculator()

字符串

ivqmmu1c

ivqmmu1c3#

这个怎么样?

  • While语句保持问题循环,直到得到有效的响应。
  • 字典有一个更有组织的年龄计算表,而不是大量的if语句。
def calculator_dog_age():

    age = 0
    while age == 0:
        age = input("Input dog years: ")
        try:
            age = int(age)
            if 20 > age > 0:
                print('A good age was entered:', age)
                age_dict = {1: 15, 2: 24, 3: 27.9, 4: 32}
                if age in age_dict:
                    return age_dict[age]
                else:
                    return age * 7.2 + (age - 5) * 7
            else:
                print('Please enter a realistic age!')
                age = 0
        except:
            print('Please enter an integer!')
            age = 0

print(calculator_dog_age())

字符串

cidc1ykv

cidc1ykv4#

#get user's input on dog_age
dog_age = input("Enter your dog's age in years")
try:
    #cast to a float
    d_age = float(dog_age)
    #convert dog age to human age
    #ensure input is not negative
    if (d_age > 0):
        if (d_age <=1):
            h_age = d_age *15
        elif (d_age <=2):
            h_age = d_age *12
        elif (d_age <=3):
            h_age = d_age*9.3
        elif (d_age <=4):
            h_age = d_age*8
        elif (d_age <=5):
            h_age = d_age*7.2
        else:
            h_age = 36 + (d_age - 5) *7    
        human_age = round(h_age,2)
        #print instruction for valid input
        print('The given dog age', dog_age,'is', str(human_age),'in human years')
    else: 
        print('Age cannot be a negative number.')
except ValueError:
    print(dog_age,'is invalid.')

字符串
我正在解决的问题有类似的条件,但需要不同的计算。我希望它能有所帮助(P.S最初我也不知道为什么我的狗年龄<0条件不起作用,原来,这一切都是关于将条件阻塞在一起并使用缩进(:)

snvhrwxg

snvhrwxg5#

如果任何人挣扎与缩进错误或“语法错误:意外的字符后,行连续字符”从打印(“\n \t '给定的狗的年龄”,年龄,“是”,d_age,“人类年。”
以下解决方案帮助了我

def calculator():
    # Get dog age
    age = input("Input dog years: ")
    try:
    # Cast to float
        d_age = float(age)
        if (d_age> 5):
            human_age = (d_age-5)*7+5*7.2
            print("The given dog age",str(d_age),"is", str(human_age), "in human years")
        elif (d_age>4):
                human_age = d_age*7.2
                print("The given dog age",str(d_age),"is",str(human_age),"in human years")
        elif (d_age>3):
                human_age = d_age*8
                print("The given dog age",str(d_age),"is",str(human_age),"in human years")
        elif (d_age>2):
                human_age = d_age*9.3
                print("The given dog age",str(d_age),"is",round(human_age,2),"in human years")
        elif (d_age>1):
                human_age = d_age*12
                print("The given dog age",str(d_age),"is",str(human_age),"in human years")
        elif (d_age>0):
                human_age = d_age*15
                print("The given dog age",str(d_age),"is",str(human_age),"in human years")
        elif (d_age<0 or d_age==0):
                print(d_age, "is a negative number or zero. Age can not be that.")
    except:
        print(age, "is an invalid age.")
        print(traceback.format_exc())
        
calculator()

字符串

ni65a41a

ni65a41a6#

希望这能有所帮助:导入追溯
def calculator():

# Get dog age
age = input("Input dog years: ")

try:
    # Cast to float
    d_age = float(age)

    # If user enters negative number, print message
    # Otherwise, calculate dog's age in human years
    if d_age < 1:
       print("Age cannot be a negative number", age)            
    else:
    
        if d_age <=1:
            human_age = 15 * d_age
        elif d_age <=2:
            human_age = 12 * d_age
        elif d_age <=3:
             human_age = 9.3 * d_age
        elif d_age <=4:
             human_age = 8 * d_age
        elif d_age <=5:
             human_age = 7.2 * d_age
        else:
            human_age = 36 + (d_age -5) * 7
        print("\n\t'The given dog age", age, "is", round(human_age, 2), "in human years.\'")
except:
    print(age, "is an invalid age.")
    print(traceback.format_exc())

字符串
calculator()#这一行调用calculator函数

输入狗龄:2

'The given dog age 2 is 24.0 in human years.'

输入狗龄:-1(负数)年龄不能为负数-1

输入狗年:n(输入一个单词)

ValueError:无法将字符串转换为float:'n'

相关问题