Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:12:48+00:00 2026-06-14T06:12:48+00:00

def parse(expression): operators= set(*/+-) numbers= set(0123456789)#not used anywhere as of now opExtrapolate= [] numExtrapolate=

  • 0
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.

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

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)If the input contains items that are not numbers or not the allowed operators then an error message is displayed

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)If the user attempts to divide by zero an error is displayed.

::What I have tried:

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:12:49+00:00Added an answer on June 14, 2026 at 6:12 am

    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

    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
    

    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 ".":
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[EDIT]: I Understand that regular expression are not made to parse XML, but my
as we see: def parse(self, response): hxs = HtmlXPathSelector(response) sites = hxs.select('//ul/li') items =
I've got this code: def __parse(self): for line in self.lines: r = Record(line) self.records[len(self.records):]
I am trying to get a webpage to parse using the following code. <var-def
I am having the below code. import xml.dom.minidom def get_a_document(name): return xml.dom.minidom.parse(name) doc =
How can I parse a String str = abc, \def,ghi\ ; such that I
def parse(self, input): input = input.replace(\n, ).replace( , ) bits = input.split(=>) return bits[:-1]
Consider this part of the grammar: def expression = SimpleExpression ~ opt(relation ~ SimpleExpression)
Trying to parse an nested expressions like GroupParser.parse({{a}{{c}{d}}}) After many hours i have now
so my code looks like this: def parse(info): 'info' is a list made out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.