在Python中实现代码错误消息 [英] Implementing error message for code in Python

查看:76
本文介绍了在Python中实现代码错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

def parse(expression):
    operators= set("*/+-")
    numbers= set("0123456789")#not used anywhere as of now
    opExtrapolate= []
    numExtrapolate= []
    buff=[]
    for i in expression:
        if i in operators:
            if len(buff) >0: #prevents buff if multiple operators
                numExtrapolate.append(''.join(buff))
            buff= []
            opExtrapolate.append(i)
            opExtrapolation=opExtrapolate
        else:
            buff.append(i)


    numExtrapolate.append(''.join(buff))

    numExtrapolation=numExtrapolate
    print(numExtrapolation)
    print("z:", len(opExtrapolation))
    return numExtrapolation, opExtrapolation

def errors():

    numExtrapolation,opExtrapolation=parse(expression)
    #Error for muliple operators
    if (len(numExtrapolation) ==3) and (len(opExtrapolation) !=2):
        print("Bad1")
    if (len(numExtrapolation) ==2) and (len(opExtrapolation) !=1):
        print("Bad2")
    #

我在一个较旧的问题中张贴了类似的代码,但是本文中提出问题的前提有所不同.

I posted similar code in an older question however the premise for questions is different in this post.

上面的代码接受用户在变量表达式中输入的数学输入,并将其拆分为操作数和运算符.如果输入不正确,错误功能将在以后打印错误.

The code above takes a mathematical input entered in a variable expression by the user and it splits it into operands and operators. The errors function will later print errors if the input is incorrect.

输入看起来像这样,其中运算符只能在set("*/+-")中,而操作数是实数.因此输入示例为45/23 + 233

Input would look something like this , where the operators can only be in the set("*/+-") and operands are real numbers. so an example input would be 45/23+233

在SO用户的帮助下,我能够解决其中一个错误(多个操作员的错误),但是我在执行更多错误消息时遇到了麻烦.

With the help of an SO user I was able to get one of the errors to work(error for multiple operators), but I am having trouble implementing a few more error messages.

1)如果输入中包含非数字或不允许的运算符的项目,则会显示错误消息

1)If the input contains items that are not numbers or not the allowed operators then an error message is displayed

2)如果用户输入的数字为.23或554之类的数字,则在小数点后或小数点后没有数字,则显示不同的错误.(请注意,像0.23这样的数字可以).

2)If a user enters a number such as .23 or something like 554. where there is no number before the decimal place or after the decimal place then a different error is displayed.(note that a number like 0.23 is fine).

3)如果用户尝试除以零,则会显示错误.

3)If the user attempts to divide by zero an error is displayed.

::我尝试过的事情:

::What I have tried:

在parse()的else语句中,我尝试在buff.append(i)上放置条件,以便仅在buff.isdigit()== true时才运行该代码,但我收到错误消息,说没有增益中的数字.我还尝试创建一个名为数字"的集合(在下面的代码中),并通过类似于初始for语句的for语句将buff.append(i)限制为该集合.但是不幸的是没有任何效果.任何帮助都将不胜感激.

In the else statement of parse(), I tried to put conditions on buff.append(i) so that it would only run that code if buff.isdigit()==true but I got errors saying that there were no digits in buff. I also tried creating a set called "numbers"(in code below) and limiting buff.append(i) to that set through a for statement similar to the initial for statement. But unfortunately nothing worked. Any and all help would be appreciated.

请不要引入比下面的代码更高级的大量代码.我正在尝试解决问题,而不是完全更改我的代码.感谢您的所有帮助.

Please don't introduce large amounts of code more advanced than the code below. I am trying to fix a problem, not completely change my code. Thanks for all your help.

推荐答案

我会给您一些提示,但不能为您解决:). 如果您需要更多,请提出一个精确的问题,我会回答.

I will give you some indications, but not solve it for you :). If you need more, ask a precise question and I'll answer it.

我给您的答案与您的代码没有直接关系.

The answers I give you are NOT directly related with your code.

您可以通过尝试将其转换为整数来测试字符串变量是否可以是整数:

You can test if a string variable can be an integer by trying to cast it :

try:
    var2 = int(var)

我让你看看它带来了什么错误

I let you see what Error it gives

对于不使用try的版本,您可以查看 isdigit方法

For a version that doesn't use try, you can look at the isdigit method

您可以通过检查字符串变量来确定它是否是您的运算符之一

You can see if a string variable if one of your operator by checking it

if (var in ["+", "-", "/", "*"])

要进行更多检查,您可以先查看变量的长度

to check even more, you can look at the variable's length first

if len(var) != and ... see above

要检查用户是否输入了.543之类的东西并拒绝它,可以查看字符串变量的第一个元素:

To check if a user inputs something like .543 and refuse it, and can look at the first element of your string variable :

if myvar[0] is ".":

要检查用户是否要除以0,只需检查最后一个数字是否等于0

To check if your user wants to divide by 0, you can simply check whether the last number is equals to 0

if int(myvar) == 0:

所有这些都希望您能够先获得运算符和数字.

All these expect you to be able to get operators and numbers first though.

另一种解决方案是在解析数字和运算符之前使用正则表达式执行这些检查. 与您要完成的练习相比,这似乎很复杂,因为它是家庭作业.无论如何,看它们可能是一个好主意.

The other solution would be to use regular expressions to perform these checks before parsing your numbers and operators. It seems quite complex compared to the exercise you are trying to achieve though as it is homework. Might be a good idea to look at them anyway.

这篇关于在Python中实现代码错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆