我有一个任务来修复程序中的错误,我不知道该怎么做,从哪里开始。你知道吗
#this function takes, as input, a list of numbers and returns the list with the following changes:
# if the original value in the list is positive, the value is doubled
# if the original value in the list is negative, the value is tripled
# if the value in the list is 0, then the value is replaced with the string “zero”
# for example, the result from timesTwo([1, 5, -2, 4, 0, -5, 3]) should be [2,10,-6,8,’zero’,-15,6]
def timesTwo(myList):
counter = 0
while (counter <= len(myList)):
if (mylist(counter) > 0:
myList[counter] = myList[counter * 2]
counter = counter + 1
elif (myList[counter] < 0):
myList[counter] = myList[counter * 3]
counter = counter + 1
else:
myList[counter] = “zero”
return myList
要查找逻辑错误,请尝试编写一些测试。您知道函数应该如何运行,因此请尝试考虑各种输入,看看函数是否提供了预期的输出。你知道吗
例如
语法和异常应该告诉您错误来自哪一行。你知道吗
为了补充其他人所说的内容,您还可以测试程序所说的应该做的事情。例如,只要给它一个由几个正数组成的列表,比如[1,2,3],并确保输出是[3,6,9]。你知道吗
什么样的错误?你知道吗
如果它们是例外,那就很容易了。Python会告诉您异常发生在哪一行,并提供完整的回溯。你知道吗
如果它们是逻辑错误,那就更难了。非常自由的使用
pdb
python调试器将有助于调试打印陈述。只是一般的分析方法。如果结果不是您希望的结果,则在必要时使用pdb
逐行遍历整个程序相关问题 更多 >
编程相关推荐