Question

So I'm writing a differential calculator program in python 2.4 (I know it's out of date, it's a school assignment and our sysadmin doesn't believe in updating anything) that accepts a user input in prefix notation (i.e. input = [+ - * x^2 2x 3x^2 x], equivalent to x^2 + 2x - 3x^2 * x) and calculates the differential.

I'm trying to find a way to read the command line user input and put the mathematical operators into a queue, but I can't figure it out! apparently, the X=input() and x=raw_input() commands aren't working, and I can find literally 0 documentation on how to read user input in python 2.4. My question is: How do I read in user input in python 2.4, and how do I put that input into a queue? Here is what I am trying:

1 formula = input("Enter Formula:")
2 
3 operatorQueue=[]
4 
5 int i = len(formula)
6 
7 for x in formula:
8     if formula[x] == '*', '+', '-', '/':
9         operatorQueue.append(formula[x])
0 
11 print "operator A:", operatorQueue.pop(0)
12 

Which is not working (I keep getting errors like "print: command not found" and "formula:command not found")

Any help would be appreciated

Était-ce utile?

La solution

@miku already answered with this being your initial problem, but I thought I would add some more.

The "sh-bang" line is required by command line scripts so that the proper process is used to interpret the language, whether it be bash, perl, python,etc. So in your case you would need: /usr/bin/env python

That being said, once you get it running you are going to hit a few other issues. raw_input should be used instead of input, because it will give you back a raw string. input is going to try and eval your string which will mostly likely give you problems.

You may need to review python syntax a bit more. Assignments in python don't require that you declare the variable type: int a = 1. It is dynamic and the compiler will handle it for you.

Also, you will need to review how to do your if elif else tests to properly handle the cases of your formula. That too wont work doing it all on one line with multiple params.

Autres conseils

If you're on a unix-ish platform, put a

#!/usr/bin/env python 

on top of your program. The shell does not seem to recognize that you are running a python script.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top