I am trying to create a RPN calculator, the addition and subtraction functions work but it only accepts numbers under 10. How can I fix this so any integers are allowed? Thanks.

import sys

accepted = '0123456789dr+-*/%^='
numbers = '0123456789'
stack = []

while len(stack) <= 20:
    while True:
        mystr = raw_input("")
        try:
            if mystr in accepted:
                for x in mystr:
                   if mystr in numbers:
                       for x in mystr:
                           mystr = int(mystr)
                           stack.append(mystr)
                if mystr == "+":
                    temp = stack.pop()+stack.pop()
                    stack.append(temp)
                if mystr == "-":
                    temp = stack.pop(-2)-stack.pop()
                    stack.append(temp)
                if mystr == "=":
                    print stack[-1]

                break
            print "Unrecognised Input."
            
        except:
           sys.exit()

Edited 7 Years Ago by TheManual: n/a

My first suggestion is to remove the giant try/except block. If you make a syntax error your program will quit and never tell you the reason why. That is extremely poor Python coding and opens the door for you to create debilitating issues in your code.

Now, in order to support numbers > 9 you'll need to basically append each numeral onto a string before doing your int() conversion.

An easy way to do this would be to have a string container... we'll call it my_numbers . In your if mystr in numbers statement, you'll be doing string concatenation (ie, my_numbers += mystr ) instead of int conversion. Then, in each of your "operator" cases, you'll take the previous my_numbers object, convert it to an int, add it to your stack, and then clear the object with my_numbers = '' .

That's only one possible way to do it... Your most efficient method would be using regular expressions, but that's probably way beyond the scope of your lesson.

Sorry, i need the try except statements.
I don't really understand what you mean. Could you link me to more information or explain in more detail? Thanks.

Sorry, i need the try except statements.

Unless your teacher is specifically telling you to use them in this manner, you really don't. And if he/she is, then I feel bad for you because your teacher is a terrible programmer.

If you're required to use them, then at the very least you should be printing any exception values like so:

try:
    # Massive try block here
except:
    print sys.exc_value
    sys.exit()

That way at least you'll have a small clue as to why your program is failing, but really you should print out the entire traceback.

I don't really understand what you mean. Could you link me to more information or explain in more detail? Thanks.

It would be something like this:

my_number = ''
for each_char in usr_input:
    if each_char in numbers:
        my_number += each_char
    else:
       stack.append(int(my_number))
       my_number = ''

Does that clear it up? Or was there something else that you didn't understand?

You need to test the logic in your if statements like
if mystr in accepted:
Here is a little test ...

accepted = '0123456789dr+-*/%^='

mystr = '2'
print(mystr in accepted)  # True

mystr = '27'
print(mystr in accepted)  # False  ouch!!!

Ok, thanks for the information. I will try that now. I was playing around and got...

stack = []

while len(stack) < 10:
    mystr = raw_input("")
    if mystr.isdigit()==True:
        stack.append(int(mystr))
        print stack
    if mystr == "print":
        print stack
    if mystr == '+':
        temp = stack.pop()+stack.pop()
        stack.append(temp)
    if mystr == "-":
        temp = stack.pop(-2)-stack.pop()
        stack.append(temp)
    if mystr == '=':
        print stack[-1]
    else:
        print "Unrecognised Input"

The functions work but it prints "unrecognised input" even though one of the if statements has been completed.

---------------------------------------------------------------------------------------------------------------------------------------------

I tried implementing your suggestion as below:

import sys

accepted = '0123456789dr+-*/%^='
numbers = '0123456789'
stack = []
newNumber = ''

while len(stack) <= 10:
    while True:
        mystr = raw_input("")     
        try:
            
            for char in mystr:
                if char in numbers:
                    newNumber += char
            stack.append(newNumber)
        
            if mystr == "+":
                temp = stack.pop()+stack.pop()
                stack.append(temp)
            if mystr == "-":

etc...
But everytime I run i get "Unrecognised Input". I think its in the right place?

Edited 7 Years Ago by TheManual: n/a

The functions work but it prints "unrecognised input" even though one of the if statements has been completed.

What functions?

The first code is not working because you're not iterating over your input anymore.

The second code is not working because your indentation is royally screwed up. Everything should be within the for loop.

This question has already been answered. Start a new discussion instead.