December 5th, 2004, 03:36 PM

Range problems
okay i hope you can help me
Here is a part of the script:
Code:
kind= raw_input ("What type of operation would you like to do?")
print
if kind not in ['+','','*','/','circ','per','tri','cir','rec','pyr','cub','con','cyl','sph','scub','spyr','ssph','den','exit']:
continue
if kind== "+":
print "You chose addition."
print
N1= input("What number would you like to add?")
print
N2= input("What is the second number you would like to add?")
A= N1+N2
print
print "________________________________________________________________________________"
print
print
print
print N1,"+",N2,"=",A,
print
how would i make it be able to also accept letters when i add because it always cancels the program when i type a letter.
How could i make it just continue and run the whole script new?
December 5th, 2004, 03:53 PM

Am I supposed to sign here?
December 5th, 2004, 03:55 PM

Did you try changing input("What number would you like to add?") and input("What is the second number you would like to add?") to raw_input's instead of just input? I think Python will still add the two strings together even if they aren't integers. Or you could make your program more complex which I did with my first program (it was for math) and have a feature that lets you choose if you're going to use letters or not. Like for algrebra. This way you could use raw_input when using your letters, and input for the integers.
December 5th, 2004, 04:04 PM

ok fine i acually wanted to avoid that because i have a whole list of commands (20)
hm ill think about the second hing that will be an upgraded program probably but how would u make something being sent to u from a popen2 thing http://forums.devshed.com/t206854/s.html
December 6th, 2004, 06:15 AM

I would do it that way, hope this helps a little bit
#First create a Dictionary
Operators = {}
#Add all Operators here
Operators['+'] = {'Name':'Addition' , 'Command':'+','Type':'BuiltIn'}
Operators[''] = {'Name':'Subtraction' , 'Command':'','Type':'BuiltIn'}
Operators['%'] = {'Name':'Remainder' , 'Command':'Remainder','Type':'Function'}
Operators['!'] = {'Name':'Factorial' , 'Command':'Factorial','Type':'Function'}
# and so on
#Add all your calculations functions here
def Remainder(N1,N2): return N1 int(N1/N2)*N2
def Factorial(n): return reduce(lambda x,y:x*y,range(1,n+1))
#A Filtering Function(Recursive) which asks for your input, remove everything but numbers, if not, it will repeat it self, this is helpful because if the user entered extra letter by mistake
def NumFilter(Question): n=raw_input(Question)
Result = ""
for i
in n:
try:
if n == '.': Result += n#Check if it got a dot
else: Result += str(int(n))#Check if the single character is a valid integer, and then append it in a string form to result
except: pass try: float(n)
#Checks if the filtered numders is a valid float, string + int with a float may cause problems
except:NumFilter(
"Please Enter a valid Number")
repeat this function over and over untill at least one true number is gives
return n
#Note you may also add a dictionary map for numbers e.g {'1':'one'} but that will get more complicated, however checking for k and replacing it with 000 would be easy
#Execution Loop
while 1:
kind = raw_input(
"What is the operator")
if kind
not in Operators.keys():
print "Unavailable Operator"
continue
print "
You choose %s \n" % (Operators[kind][
'Name'])
#Calls the Operator Name
N1=NumFilter("
What number would you like to do %s with ?" % (Operators[kind][
'Name']))
#Uses the Operator name or you might extend the Dictionary to include anything else
N2 = NumFilter("
What is the second number you would like to do %s with" % ( Operators[kind][
'Name'] ))
print "\n%s\n\n\n" % (
""*50)
#prints  50 times and couple of spacing lines
#Checking the type key if it's a Function, it will put the equation is an a functional style e.g f(x,y)
if Operators[kind]['Type'] ==
'Function':
Equation = Operators[kind]['Command'] + "(%s,%s)" %(N1,N2)
#using x+y style
elif Operators[kind][
'Type'] ==
'BuiltIn':
Equation = N1 + Operators[kind]['Command'] + N2
#You can add unlimited types or something
Also for printing I used the Equation string, then eval() which will execute the string
also you might print inside IF in case you wanted more special outputs/calculations
print "%s = %s" % (Equation,eval(Equation))
After all I prefer doing it in OO style but this should also work, tell me your results!!!!
December 6th, 2004, 07:12 AM

how would i make it be able to also accept letters when i add because it always cancels the program when i type a letter.
You could use:
Code:
n = None
try:
n = input("Enter a number: ")
except:
print "That's not a number."
It would let you type letters without it closing the program.
I think Python will still add the two strings together even if they aren't integers.
It will, but it will just do string concatenation on them.
Code:
>>> 1+1
2
>>> "1" + "1"
'11'