954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Urgent!!!!!!URGENTTTTT!

code:
def rpn():
list = []
while 1:
x = raw_input(" ")
if x in ["+","-","*","/","%"]:
y=str(eval(list[-2]+x+list[-1]))
list[-2:]=[y]
elif x == "":
break
elif x == "=":
print y
else:
list.append(x)

How can i modify my rpn calculator in order to:

1)accept random number

x = random number

e.g

x x x x x x

2)accept a stack variable
and pop each time a number:
e.g

1
2
3
+
stack
+
stack
=


3)accept comments

e.g

#comment
1 #comment
2 #comment
+
=

4) accept as input :

2
+
=


5) accept exponential

e.g

1 1 ^ 1 =


Please help me! Thank you!

im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

no - one ???

im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

for Python on Daniweb:
Please use the [code=python] and [/code] tag pair to enclose your python code.

Python code uses proper indentation as part of its code. Presenting unindented Python code forces the person that may want to help you to do this and discourages many.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

[Sorry for not including the code tags in my previous post]

Well thats my rpn calculator up to now
but i still want to add extra features such as

a)to insert a variable, for instance "x", while im doing some calculations in order to re-add the two previous numbers every time that im typing x :
1
2
3
x
+
x
=

b) to be able to inputs such as:
1
+


c) to handle zeroDivisionError

d)to be able to handle exponential functions, in the same line not as my current version of rpn works i.e:

2 2 ^ 2 ^ =

e) to be able to accept comments in the same manner as the above works i.e:

#accept a comment
1 1 + #accept a comment


f) to be able to accept random numbers


Thanks in advance and i'm awaiting tensely for replies ! ! !

import random
import math

def rpn_calculator():
    list = []
    ls = []
  
    while 1:
        
        x = raw_input("> ")
        ls.append(x)


            
        #BASIC THINGS
        
        if x in ["+","-","*","/","%"]:
            y=str(eval(list[-2]+x+list[-1]))
            list[-2:]=[y]

     
            #add comments
        
        elif x in ["+","#"]:
                for i in range(len(ls)):
                    if list["#":] + '\n':
                        pass

                elif x in ['\n', list["#":]]:
                        ls.remove(x)
                        pass

                elif x not in list["#":]:
                        y = str(eval(list[-2] + x + list[-1]))
                        list[-2:] = [y]
                        
            
        elif x == "":
            break
        elif x == "=":
            print y

        else:
            list.append(x)
im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

Sorry, but could you please be more specific with what your program does? I have no idea what a 'rpn' calculator does, neither what it is. If you could explain what your calculator is supposed to do, I believe I can be of some help =]

Azurea
Light Poster
38 posts since Oct 2007
Reputation Points: 10
Solved Threads: 4
 

http://en.wikipedia.org/wiki/Reverse_Polish_notation

Its a calculator that takes input of this form:

a b + = result

Thanks in advance !

im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

One quick hint, do not use list as a variable name it is a function in Python. Use something like mylist.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

I did it but i cant find out how will this help me ?

im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

Have you ever gotten your code to do anything? The way it is now it will not work, even in its simplest form.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

Yeah it works with the simple things except from those i posted previously.

def rpn_calculator():
    mylist = []
    ls = []
    
    while 1:

        x = raw_input("> ")
        ls.append(x)

        
            #basic things
                    
        if x in ["+","-","*","/","%"]:
            y=str(eval(mylist[-2]+x+mylist[-1]))
            mylist[-2:]=[y]

            #comments

        elif x in ["+","#"]:
            for i in range(len(ls)):
                if mylist["#":]+ '\n':
                    pass
                elif x in ['\n', mylist["#":]]:
                    ls.remove(x)
                    pass
                elif x not in mylist["#":]:
                    y = str(eval(mylist[-2] + x + mylist[-1]))
                    mylist[-2:] = [y]
                        
        elif x == "":
            break
        elif x == "=":
            print y
        else:
            mylist.append(x)
im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

hint: use a stack. number => push onto stack ; operator => pop 2 off stack, operate, push back result.

dang42
Newbie Poster
5 posts since Nov 2007
Reputation Points: 10
Solved Threads: 2
 

Neither can i understood the modification of list to mylist..

eleonora
Light Poster
48 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 
hint: use a stack. number => push onto stack ; operator => pop 2 off stack, operate, push back result.

But thats how the calculator works
The problem is that i dunno how to add the extra features i posted previously

Thanks in advance !

im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

No-one ???
:(

im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

Sorry, I thought I said I could help, but this is out of my league, and I can't think of how to do it xD

Azurea
Light Poster
38 posts since Oct 2007
Reputation Points: 10
Solved Threads: 4
 
Sorry, I thought I said I could help, but this is out of my league, and I can't think of how to do it xD

Okay thanks anyway...

Does anyone else know how to help me ???
:(

im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 
Neither can i understood the modification of list to mylist..

You don't want to use a Python builtin function as a variable name. Here is an example, first the normal use of the list() function ...

print list('abcde')
"""
my output -->
['a', 'b', 'c', 'd', 'e']
"""


Now make list is a variable name, this will give you an error when you want to use it as a function later ...

list = []
# lots 
# of your
# code here
# finally comes the moment you want to use list as a function 
print list('abcde')
"""
my output -->
TypeError: 'list' object is not callable
"""
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

Actually the problem that i want to solve is to find a way the input of my program to accept apart from vertical, horizontal inputs
in other words the current form accepts
1
2
+
=
but i want to modify it in order to accept:
1 2 +


Thanks

im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

http://en.wikipedia.org/wiki/Reverse_Polish_notation

Its a calculator that takes input of this form:

a b + = result

Thanks in advance !

Write your initial program to do just that. Once you got that working, expand gradually with more operators.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

But my program is supposed to do both of them
To accept things either vertical or horizontal
:-/

im_desperate
Newbie Poster
20 posts since Nov 2007
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You