![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
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 |
|
|
|
|
|
|
#12 | |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Quote:
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) |
|
|
|
|
|
|
#13 |
|
Programmer
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3
![]() |
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... |
|
|
|
|
|
#14 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#15 |
|
Hobbyist Programmer
Join Date: Dec 2005
Posts: 118
Rep Power: 0
![]() |
As you can see in Arevos' code, they're functions already in Python.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|