Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 7th, 2006, 6:52 PM   #11
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
It might be better in this case to use functions.
What does that mean? Replace the arithmetic operator with a function?
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 8th, 2006, 1:33 AM   #12
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by DaWei
What does that mean? Replace the arithmetic operator with a function?
He probably means something like:
from operator import add, sub, mul, div
operators = { '+' : add, '-' : sub, '*' : mul, '/' : div }

op = raw_input("Please enter the prefered operator: +, -, *, /")

a = int(raw_input("Please enter a number:"))
b = int(raw_input("Please enter another number:"))
print "Answer = " + operators[op](a, b)
Arevos is offline   Reply With Quote
Old Feb 8th, 2006, 11:20 AM   #13
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
Actually i meant something like this: (I only did one operation, but u get the point.)
 
def addition(a,b):
     return a+b
print "Select Option: "
print "a = addition"
choice = raw_input("Enter selection: ")
if choice == a:
     firstNum = input("Enter first number: ")
     secNum = input("Enter second number: ")
     addition (firstNum, secNum)

It was just a thought...
snipertomcat is offline   Reply With Quote
Old Feb 8th, 2006, 11:35 AM   #14
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
That's what I was afraid you meant. You're fortunate you have more computational power than you need, so you can be profligate with it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Feb 8th, 2006, 12:24 PM   #15
Klipt
Hobbyist Programmer
 
Join Date: Dec 2005
Posts: 118
Rep Power: 0 Klipt is an unknown quantity at this point
As you can see in Arevos' code, they're functions already in Python.
Klipt is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC