gpt4 book ai didi

python - 运行代码不提供任何输出

转载 作者:太空宇宙 更新时间:2023-11-04 10:50:15 24 4
gpt4 key购买 nike

我有一个简化的程序,看起来像这样:

def listAssign(lst,index,item):
"""Assigns item item to list lst at index index, returns modified list."""
lst[index] = item
return lst

def listInsert(lst,index,item):
"""Inserts item item to list lst at index index, returns modified list."""
lst.insert(index.item)
return lst

# ...

def listSurgery(lst,indices,f,*extraArgs):
"""Performs operation f on list lst at depth at indices indices, returns modified list."""
parent = lst
for index in indices[:-1]:
parent = parent[index]
parent = f(parent,indices[-1],*extraArgs)
return listSurgery(lst,indices[:-1],listAssign,parent)

# ...

def parseStringToList(s):
"""Takes in a user-input string, and converts it into a list to be passed into parseListToExpr."""
for c in s[:]: # Removes extra spaces from beginning of string
if c == ' ':
s = s[1:]
else:
break
for c in s[::-1]: # Removes spaces from end of string
if c == ' ':
s = s[:-1]
else:
break
l = [] # List to build from string; built by serially appending stuff as it comes up
b = True # Bool for whether the parser is experiencing spaces (supposed to be True if last character processed was a space)
t = False # Bool for whether the parser is experiencing a string of non-alphanumeric characters (supposed to be True if last character was a non-alphanumeric character)
lvl = 0 # Keeps track of depth at which operations are supposed to be occurring
for c in s:
if c == ' ': # If the character is a space, do nothing, but make sure that it's documented that the last character processed was a space
b = True
elif c == '(' or c == '[': # If the character is an opening parenthesis, add a new level of depth to the list, by appending another list to the lowest level and incrementing lvl
l = listSurgery(l,[-1]*(lvl+1),listInsert,[])
lvl += 1
if c == '[': # ] (left here for overzealous parsers) If the bracket is square, also append a comma as the first element of the appended list
l = listSurgery(l,[-1]*(lvl+1),listInsert,',')
elif c == ')' or c == ']': # If it's a closing parenthesis, make it stop paying attention to the list it's working on
lvl -= 1
elif c == ',': # If it's a comma, then append it to the list as a separate element and start a new string to append characters to
l = listSurgery(l,[-1]*(lvl+1),lambda x,y,z:listAssign(x,y,x[y]+z),[',',''])
elif not c.alnum(): # If it's non-alphanumeric and not any of the above, then append it to string that it's working on if it's a non-alphanumeric string
if not t: # If the string that it's working on isn't non-alphanumeric, then finish working on that string and append a new string to work on
l = listSurgery(l,[-1]*(lvl+1),listInsert,'')
l = listSurgery(l,[-1]*(lvl+1),lambda x,y,z:listAssign(x,y,x[y]+z),c)
t = True
else: # If the character is alphanumeric, append it to the string it's working on
assert c.isalnum()
if b or t: # If the string it's working on isn't alphanumeric or doesn't exist, append a new string
l = listSurgery(l,[-1]*(lvl+1),listInsert,'')
b, t = False, False
l = listSurgery(l,[-1]*(lvl+1),lambda x,y,z:listAssign(x,y,x[y]+z),c)
return l

# ...

cmds = {
'help':"List the available functions.",
'quit':"Quit this application.",
'exit':"Exit this application (exactly the same thing)."
}

print "MAT (Michael's Analysis Tool), version 0.0.0"

op = '' # Declare string to use for input
while op != 'exit' and op != 'quit': # Keep a REPL unless the user types "exit" or "quit", in which case exit
op = raw_input("> ")
if op == 'help':
print "The commands available in addition to free evaluation are:"
for item in cmds:
print ' ', item + ":", cmds[item]
elif op == 'quit' or 'exit':
pass
else:
print str(parseStringToList(op))

当我使用helpquitexit 作为命令时,结果很好。但是当我给它任何其他类型的输入时,比如 1 + 1 = 2 (应该引出 ['1','+','1','=','2' ]) 或 This is a sentence.(应该引出 ['This','is','a','sentence','.'],它不打印任何内容,只是要求更多输入。(也就是说,它在功能上等同于该程序的最后一行被替换为 continue。)我不明白为什么。 parseStringToList 以一种非常直接的方式定义。它应该至少返回[],即它返回的变量的原始值。但相反,它不打印任何东西。为什么?(我明天会尝试更彻底的调试,但到目前为止我的努力还没有成功。)

最佳答案

你需要改变这个:

elif op == 'quit' or 'exit':

对此

elif op == 'quit' or op == 'exit':

关于python - 运行代码不提供任何输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14433270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com