2015-10-20 223 views
0

我是一名初级程序员,学习Python 3.4.3。我正在编写一个程序,用于标记表示数学公式的字符串,或将字符串转换为列表。其他语句与if语句一起执行

我正在使用一系列if/elif/else语句来完成此操作。我这个函数的代码如下:

def positive_or_add(usr_string, place): 
    """ Determines if a + or - is intended to be a add /subtract or positive 
    or negative. Takes a string (usr_string) and an integer (place) to 
    determine the place in the string. Returns true for an operator, or 
    negative for pos/neg 
    (string) -> bool 
    >>> '3 + 5 +(-4) 
    Fales 
    """ 
    #munover the place setting prior to the +/- operator 
    place -= 1 
    #munover left through the white space to determine what preceeded 
    while usr_string[place] == ' ': 
     place -= 1 
    if usr_string[place].isdigit() or usr_string[place] == ')': 
     return True 
    else: 
     return False 

def tokenize(usr_string): 
    """ 
    (str) -> list 
    Takes a string, and tokenizes it. Basically turning the string 
    representing a mathimatical formual and making it into a list, eliminating 
    the white spaces 
    >>> '3 + 5 + (-4) 
    [3, +, 5, +, (, -4,)] 
    """ 
    token_list = [] 
    i = 0 
    while i < len(usr_string): 
#Add values to the list that arent white space, + or - 
     if usr_string[i] != ' ' and usr_string[i] != '+' and usr_string[i] != '-': 
      token_list.append(usr_string[i]) 
      i += 1 
     elif usr_string[i] == '+' or usr_string[i] == '-': 
#Determine if that + or - is an operator or pos/neg 
      sign = positive_or_add(usr_string, i) 
      if sign == True: #Operator 
       token_list.append(usr_string[i]) 
       i += 1 
      elif sign == False: #pos/neg 
       token_list.append(usr_string[i] + usr_string[i + 1]) 
       i += 2 
     elif usr_string == ' ': 
      continue 
     else: 
      print('something messed up') 
      i += 1 
    return token_list 

#Test 
print(tokenize('3 + 5 + (-4)')) 

我的问题是,我else语句总是执行,与if/elif的语句一起。我基本上得到了'弄乱了事情'的几行,然后是我生成的列表tonek_list。

当我试图擦除else语句时,程序基本上给了我一个空行,没有任何反应。

我猜我只是没有看到这里的东西。任何帮助将不胜感激。

+0

请在此处粘贴positive_or_add函数 –

+0

您的输入字符串包含parens,标记器似乎无法处理p阿伦斯。 – ShadowRanger

+0

我很抱歉,我不确定Parens是什么(还没有,我猜...) – DntMesArnd

回答

1

在这里,你有固定的代码:

def tokenize(usr_string): 
    """ 
    (str) -> list 
    Takes a string, and tokenizes it. Basically turning the string 
    representing a mathimatical formual and making it into a list, eliminating 
    the white spaces 
    >>> '3 + 5 + (-4) 
    [3, +, 5, +, (, -4,)] 
    """ 
    token_list = [] 
    i = 0 
    while i < len(usr_string): 
#Add values to the list that arent white space, + or - 
     if usr_string[i] != ' ' and usr_string[i] != '+' and usr_string[i] != '-': 
      token_list.append(usr_string[i]) 
      i += 1 
     elif usr_string[i] == '+' or usr_string[i] == '-': 
#Determine if that + or - is an operator or pos/neg 
      sign = positive_or_add(usr_string, i) 
      if sign == True: #Operator 
       token_list.append(usr_string[i]) 
       i += 1 
      elif sign == False: #pos/neg 
       token_list.append(usr_string[i] + usr_string[i + 1]) 
       i += 2 
     elif usr_string[i] == ' ': 
      i += 1 
      continue 
     else: 
      print('something messed up') 
      i += 1 
    return token_list 

修正:
- 的sr_string[i]代替sr_string(26行)
- 需要的i += 1continue之前的缩进,否则无限循环

+0

我的无限循环是由于对于任何感兴趣的人在elif usr_string [i] ==''语句中不包括i + = 1而引起的。 – DntMesArnd

+0

确实,并且代码将在固定@DntMesArnd之后生效。当看到它时,才意识到这一点 –

3

立即解决问题几乎肯定与这条线在这里做:

elif usr_string == ' ': 

正在对usr_string[i]执行的每个其他检查,所以我不知道到底是什么原因促使你检查整个该特定情况下的字符串。它的意思是,只要你在输入字符串中找到一个空格(并且整个字符串不只是一个空格),它就会落入你的“某些不妥之处”的情况。

此外,这是唯一不会增加索引变量i的情况,因此您找到的第一个空间(一旦您修复了第一个问题)将使您陷入无限循环。

我想你会发现事情会顺畅很多,如果你把它改为:

elif usr_string[i] == ' ': 
    i += 1 

甚至有一次这些问题是固定的,看来你没有代码正确处理括号()。现在,我建议将它们从输入字符串中取出以便测试更基本的表达式,但是您可能需要在某个时刻处理它们。

+0

此外,除了最高级别的测试之外,它似乎忽略了空白的可能性,并且只在没有空白的情况下正确处理parens。没有详细检查它,代码是...很难遵循。 – ShadowRanger

+0

这当然解决了else语句的问题。谢谢!只是一个打字错误。现在,当我运行代码时,我只得到一个空行。 – DntMesArnd

+0

我的目标是在结果列表中不包含空格。一旦我得到它运行,我想我需要通过它,看看是否有更好的方法... – DntMesArnd