|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
||||
|
||||
|
Read about exception handling: http://docs.python.org/tut/node10.html
__________________
Am I supposed to sign here?
|
|
#3
|
||||
|
||||
|
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.
|
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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)#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")After all I prefer doing it in OO style but this should also work, tell me your results!!!! |
|
#6
|
||||
|
||||
|
Quote:
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. Quote:
It will, but it will just do string concatenation on them. Code:
>>> 1+1 2 >>> "1" + "1" '11' |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Range problems |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|