其他语句与if语句一起执行

时间:2015-10-20 00:24:41

标签: python

我是一名学习Python 3.4.3的初学程序员。我正在编写一个程序,用于对表示数学公式的字符串进行标记,或将字符串转换为列表。

我正在使用一系列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语句时,程序基本上给了我一个空白行,没有任何反应。

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

2 个答案:

答案 0 :(得分:3)

你的直接问题几乎当然与此行有关:

elif usr_string == ' ':

正在usr_string[i]上执行所有其他检查,因此我不确定是什么原因促使您检查该特定情况的整个字符串。这意味着,每当你在输入字符串中找到一个空格(并且整个字符串不仅仅是一个空格)时,它就会落入你的错误状态。情况下。

此外,这是您唯一没有递增索引变量i的情况,因此您找到的第一个空格(一旦您修复了第一个问题)将放置你进入一个无限循环。

如果您将其更改为:

我认为您会发现事情会变得更加顺畅
elif usr_string[i] == ' ':
    i += 1

即使这两个这些问题都得到修复,您似乎也没有正确处理括号()的代码。现在,我建议将它们从输入字符串中删除,以便测试更基本的表达式,但您可能需要在某些时候处理它们。

答案 1 :(得分: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 += 1之前需要缩进continue,否则无限循环