![](/img/trans.png)
[英]Why does my code throw "NameError: name 'ModuleNotFoundError' is not defined" error?
[英]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 开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.