为什么我的代码会抛出一个非常奇怪的错误代码?
Why does my code throw up an extremely odd error code?
我最近在做佐治亚理工学院的 CS1301xII。然而,这个问题的错误信息很奇怪
mystery_list = ["Taylor Swift", "Twenty Two", "Georgia Tech"]
#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.
#Above is a list of strings. Don't worry if this syntax is a
#little unfamiliar, we'll talk you through it and then cover
#it more in chapter 4.3.
#
#Write some code that will count the number of instances of
#the letter 't' in the list of strings. Count both capital
#'T' and lower-case 't'. Then, print the number of instances
#of the letter 't'.
#
#For example, with the list declared above, you would print
#6: two in the first string, three in the second, one in the
#third.
#
#Because we haven't used lists very extensively, we've
#gotten you started. The loop below will iterate through each
#string in the list. Next, you want to iterate through each
#letter in the current string, check if it's a t, and
#increment a counter if so.
#You'll want to add some code before the loop here.
counter = 0
listnum = 0
listletter = 0
for string in mystery_list:
while listnum <= len(mystery_list):
if mystery_list[listnum][listletter] == "T" or mystery_list[listnum][listletter] == "t":
listletter += 1
if listletter > len(mystery_list[listnum]):
listletter = 0
listnum += 1
这是错误信息:
../resource/scripts/run.sh: line 1: 669 Killed python3 $VOC_SELECTED_FILE
Command exited with non-zero status 137
如果这是一个常见的错误,比如语法错误或被零除,或者几乎每一个错误,它都会给我非零状态 1。
所以我的问题是:
1. 如何修复我的代码
2. 这个错误信息到底是什么意思???
您的代码中存在无限循环。 listnum 始终为 0,导致无限 while 循环。所以服务器正在终止你的脚本并给出奇怪的错误。
这是一个简单的解决方案:
mystery_list = ["Taylor Swift", "Twenty Two", "Georgia Tech"]
counter = 0
for string in mystery_list:
counter+=string.lower().count("t")
已根据您的代码进行编辑。
mystery_list = ["Taylor Swift", "Twenty Two", "Georgia Tech"]
counter = 0
listnum = 0
listletter = 0
for string in mystery_list:
while listnum < len(mystery_list):
print('Letter:',mystery_list[listnum][listletter],'sTRING: ',mystery_list[listnum])
if mystery_list[listnum][listletter] == "T" or mystery_list[listnum][listletter] == "t":
counter += 1 # increment counter only if T matches
listletter +=1 # iterete forworward no matter what
if listletter >= len(mystery_list[listnum]):
listletter = 0
listnum += 1
变化:
- 您忘记递增
counter
,而是递增迭代器变量。
listletter
重置条件必须是 >=
因为索引从 0 开始。
我最近在做佐治亚理工学院的 CS1301xII。然而,这个问题的错误信息很奇怪
mystery_list = ["Taylor Swift", "Twenty Two", "Georgia Tech"]
#You may modify the lines of code above, but don't move them!
#When you Submit your code, we'll change these lines to
#assign different values to the variables.
#Above is a list of strings. Don't worry if this syntax is a
#little unfamiliar, we'll talk you through it and then cover
#it more in chapter 4.3.
#
#Write some code that will count the number of instances of
#the letter 't' in the list of strings. Count both capital
#'T' and lower-case 't'. Then, print the number of instances
#of the letter 't'.
#
#For example, with the list declared above, you would print
#6: two in the first string, three in the second, one in the
#third.
#
#Because we haven't used lists very extensively, we've
#gotten you started. The loop below will iterate through each
#string in the list. Next, you want to iterate through each
#letter in the current string, check if it's a t, and
#increment a counter if so.
#You'll want to add some code before the loop here.
counter = 0
listnum = 0
listletter = 0
for string in mystery_list:
while listnum <= len(mystery_list):
if mystery_list[listnum][listletter] == "T" or mystery_list[listnum][listletter] == "t":
listletter += 1
if listletter > len(mystery_list[listnum]):
listletter = 0
listnum += 1
这是错误信息:
../resource/scripts/run.sh: line 1: 669 Killed python3 $VOC_SELECTED_FILE Command exited with non-zero status 137
如果这是一个常见的错误,比如语法错误或被零除,或者几乎每一个错误,它都会给我非零状态 1。 所以我的问题是: 1. 如何修复我的代码 2. 这个错误信息到底是什么意思???
您的代码中存在无限循环。 listnum 始终为 0,导致无限 while 循环。所以服务器正在终止你的脚本并给出奇怪的错误。
这是一个简单的解决方案:
mystery_list = ["Taylor Swift", "Twenty Two", "Georgia Tech"]
counter = 0
for string in mystery_list:
counter+=string.lower().count("t")
已根据您的代码进行编辑。
mystery_list = ["Taylor Swift", "Twenty Two", "Georgia Tech"]
counter = 0
listnum = 0
listletter = 0
for string in mystery_list:
while listnum < len(mystery_list):
print('Letter:',mystery_list[listnum][listletter],'sTRING: ',mystery_list[listnum])
if mystery_list[listnum][listletter] == "T" or mystery_list[listnum][listletter] == "t":
counter += 1 # increment counter only if T matches
listletter +=1 # iterete forworward no matter what
if listletter >= len(mystery_list[listnum]):
listletter = 0
listnum += 1
变化:
- 您忘记递增
counter
,而是递增迭代器变量。 listletter
重置条件必须是>=
因为索引从 0 开始。