我有一個任務來修復程序中的錯誤,我不知道該如何去做以及從哪裏開始。如何找到並修復錯誤
#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
你的意思是除了編寫它試圖在文字處理器中編寫Python代碼的人嗎? – geoffspear
使用調試,逐行逐行瀏覽並觀察所有變量。一般來說,從哪裏開始將「學習更多的Python」,答案將變得清晰,我敢肯定。 –
你不需要括號(即'('和')'),while和if語句。你似乎在第一個'if'中混淆了你自己,只有'('沒有匹配的paren,然後使用parens而不是括號(例如'['和']')。嘗試去除這些測試中的parens ,而且錯誤會變得更清晰可見。 – tehwalrus