第一次作业,我已经尝试了一段时间



我很难找出我的代码有什么问题。这是我正在学习的课程的第一项作业。起初,我只做了11分,我能够修复当时显示的错误,并得到22分,之后,我又修复了它并得到了56分,但我不能再进一步了。这是我的代码:

import traceback

def calculator():

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

我得到的错误是:

已从您的程序收到以下输出:

' "给定的狗年龄 1.0 是人类的 15 岁。" '

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-84-21926286aa29> in <module>
7 print("Received the below output from your program:n---------------n'{}'".format(checker.test_one(1)))
8 checker.assert_almost_equal(
----> 9     checker.parse_dog_age(checker.test_one(1)),
10     15,
11     "Expected a 1 year old dog to be 15 in human years. Make sure your print statement is formatted as:nt 
~/work/release/module 1b/checker.py in parse_dog_age(msg)
25 def parse_dog_age(msg):
26     human_age = re.findall("d+.d+ in human", msg)
---> 27     human_age = human_age[0].split(' ')[0].strip()
28     return float(human_age)
29 
IndexError: list index out of range

已从您的程序收到以下输出:

' "给定的狗年龄 3.0 是 27.9000000000000002 在人类年。 '

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-86-94d85e6cbf89> in <module>
8 checker.assert_almost_equal(checker.parse_dog_age(checker.test_three(3)), 27.9,
9             "Expected a 3 year old dog to be 27.9 in human years. Make sure your print statement is formatted as:nt 
---> 10     'The given dog age <input_value> is <calculated_age> in human years.'")
11 print("Success!")
/opt/conda/lib/python3.7/unittest/case.py in assertAlmostEqual(self, first, second, places, msg, delta)
895                 places = 7
896 
--> 897             if round(diff, places) == 0:
898                 return
899 
TypeError: 'str' object cannot be interpreted as an integer

您收到的错误似乎不是来自您的脚本。

正如我所评论的,您的脚本运行良好,我没有看到任何问题。您的错误完全来自其他文件:/opt/conda/lib/python3.7/unittest/case.py/work/release/module 1b/checker.py.

为什么这些程序正在运行并导致错误?是否将编写的代码提交到这些脚本?从错误中可以得到一个提示,狗的年龄应该是一个int,而不是一个float


TypeError 回溯(最近一次调用) 在 8 checker.assert_almost_equal(checker.parse_dog_age(checker.test_three(3)), 27.9, 9 "预计一只 3 岁的狗在人类年龄为 27.9 岁。确保打印语句的格式为:\t
---> 10"给定的狗年龄以人类年龄。") 11 打印("成功!

而您将其打印为浮点数

"给定的狗年龄 3.0 是 27.9000000000000002 在人类年。"'

另外,两端都应该有引号吗?

您的第一个错误来自以下行:human_age = human_age[0].split(' ')[0].strip(),其中human_age要么是一个空字符串,要么是一个仅由空格组成的字符串。

简而言之,在不知道程序的预期行为或源代码的情况下调试程序是非常困难的。考虑发布 MRE。

最新更新