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 8732423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:26:12+00:00 2026-06-13T09:26:12+00:00

I am quite new to programming. This is in relation to python. So the

  • 0

I am quite new to programming. This is in relation to python. So the idea is to take an expression such as 3/5 or, at most, 3/5*2(at most two operators, note that the operators can be any of +,-,/,*) and solve it. White space can exist anywhere within the expression.

The user enters the expression, say 3/5, and the program needs to solve the expression and display the answers. What I have tried is below. Note, I only have attempted the first part, once I can properly split the original expression that the user enters(which would be a string), creating functions would be the easy part:

expres= str(input("something:"))

ssplit= hit.partition("/")
onec= int((ssplit[0].lstrip()).rstrip())
twoc= (ssplit[1].lstrip()).rstrip()
threec= int((huns[2].lstrip()).rstrip())


print(onec,"...",twoc,"...",threec) #just a debug test print

So above, I can take an expression like 3/5 and split it into three separate strings:3 , /, and 5. I can also remove all whitespace before and after the operators/operands. I am having problems with splitting expressions like 4/5+6, Because I can’t put code in for ssplit[3] or ssplit[4] and then enter an expression like 3/5, because it won’t be defined. Basically I needed you help to find out how to split an expression like 3/4-6,etc. I also need help with the line “ssplit= hit.partition("/")” so that it will look at the entered expression and work with +,-, and * as well. Any and all help is appreciated. Also if my code above looks nasty and inefficient please give me criticism. Thanks!

Note I can’t, and wouldn’t want to use eval.
Order of operations is required.
I cant use complicated commands. I need to keep it simple, the most I can use is string libraries, converting between strings/integers/floats etc. and if,and,etc. statements. I can also use functions.

  • 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-13T09:26:13+00:00Added an answer on June 13, 2026 at 9:26 am

    If I wasn’t going to rely on external libraries, I’d do it something like this:

    def parse(x):
        operators = set('+-*/')
        op_out = []    #This holds the operators that are found in the string (left to right)
        num_out = []   #this holds the non-operators that are found in the string (left to right)
        buff = []
        for c in x:  #examine 1 character at a time
            if c in operators:  
                #found an operator.  Everything we've accumulated in `buff` is 
                #a single "number". Join it together and put it in `num_out`.
                num_out.append(''.join(buff))
                buff = []
                op_out.append(c)
            else:
                #not an operator.  Just accumulate this character in buff.
                buff.append(c)
        num_out.append(''.join(buff))
        return num_out,op_out
    
    print parse('3/2*15')
    

    It’s not the most elegant, but it gets you the pieces in a reasonable data structure (as far as I’m concerned anyway)

    Now code to actually parse and evaluate the numbers — This will do everything in floating point, but would be easy enough to change …

    import operator
    def my_eval(nums,ops):
    
        nums = list(nums)
        ops = list(ops)
        operator_order = ('*/','+-')  #precedence from left to right.  operators at same index have same precendece.
                                      #map operators to functions.
        op_dict = {'*':operator.mul,
                   '/':operator.div,
                   '+':operator.add,
                   '-':operator.sub}
        Value = None
        for op in operator_order:                   #Loop over precedence levels
            while any(o in ops for o in op):        #Operator with this precedence level exists
                idx,oo = next((i,o) for i,o in enumerate(ops) if o in op) #Next operator with this precedence         
                ops.pop(idx)                        #remove this operator from the operator list
                values = map(float,nums[idx:idx+2]) #here I just assume float for everything
                value = op_dict[oo](*values)
                nums[idx:idx+2] = [value]           #clear out those indices
    
        return nums[0]
    
    print my_eval(*parse('3/2*15'))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry if this is obvious, but I'm quite new to python programming, why won't
I'm quite new to that functional programming paradigm, but so far I like it.
This is in regards to C# coding, im quite new to this programming language,
I'm quite new to javascript programming and got stuck in this problem: I have
I'm quite new to Java Programming and am writing my first desktop app, this
I am quite new to python programming (C/C++ background). I'm writing code where I
I am quite new to programming, but most of it I am new when
I'm quite new into the programming game, I've been working on this program for
So I'm quite new to programming in general, so this may be a stupid
I'm dealing with an issue that's driving me crazy. I'm quite new in programming

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.