Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 5th, 2004, 03:36 PM
monkeyman23555 monkeyman23555 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2004
Location: There where the rabbits jump
Posts: 556 monkeyman23555 User rank is Corporal (100 - 500 Reputation Level)monkeyman23555 User rank is Corporal (100 - 500 Reputation Level)monkeyman23555 User rank is Corporal (100 - 500 Reputation Level)monkeyman23555 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 51 m 15 sec
Reputation Power: 5
Send a message via MSN to monkeyman23555
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?

Reply With Quote
  #2  
Old December 5th, 2004, 03:53 PM
Boki's Avatar
Boki Boki is offline
A wanna-be guru of some sort
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2004
Location: Either online or offline
Posts: 624 Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level)Boki User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 4 h 7 m 13 sec
Reputation Power: 14
Read about exception handling: http://docs.python.org/tut/node10.html
__________________
Am I supposed to sign here?

Reply With Quote
  #3  
Old December 5th, 2004, 03:55 PM
Yegg`'s Avatar
Yegg` Yegg` is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Dec 2004
Location: Meriden, Connecticut
Posts: 1,731 Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level)Yegg` User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Weeks 6 Days 4 h 43 sec
Reputation Power: 68
Send a message via AIM to Yegg`
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.

Reply With Quote
  #4  
Old December 5th, 2004, 04:04 PM
monkeyman23555 monkeyman23555 is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2004
Location: There where the rabbits jump
Posts: 556 monkeyman23555 User rank is Corporal (100 - 500 Reputation Level)monkeyman23555 User rank is Corporal (100 - 500 Reputation Level)monkeyman23555 User rank is Corporal (100 - 500 Reputation Level)monkeyman23555 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 20 h 51 m 15 sec
Reputation Power: 5
Send a message via MSN to monkeyman23555
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

Reply With Quote
  #5  
Old December 6th, 2004, 06:15 AM
weam weam is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 73 weam User rank is Private First Class (20 - 50 Reputation Level)weam User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 23 h 52 m 46 sec
Reputation Power: 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)
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!!!!

Reply With Quote
  #6  
Old December 6th, 2004, 07:12 AM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 9
Quote:
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.

Quote:
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'

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Range problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT