![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Opinion's wanted on some code
I just wrote this program and it's my first actual program with a use.I
want to know what you think please let me know and if i can improve it i want to know how. its a basic calculator print "the legable symbols are '+','-','/' and '*'"
print "it on a 10 loop so you can use it 10 times"
print
loop = 0
while (loop < 10):
loop = loop + 1
def plus():
print var_1, "+", var_3, "=", var_1 + var_3
def min():
print var_1, "-", var_3, "=", var_1 - var_3
def multi():
print var_1, "*", var_3, "=", var_1 * var_3
def div():
print var_1, "/", var_3, "=", (var_1 + 0.0) / var_3
var_1 = input ("Type first number:")
var_2 = raw_input ("Type Operation:")
var_3 = input ("Type second number:")
if var_2 == "+": plus()
if var_2 == "-": min()
if var_2 == "*": multi()
if var_2 == "/": div()
print |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Well, it's a good start. I would take the function defines out of the loop, so the interpreter has to do them only once. Then add an option to exit the while loop. Avoid the function input() and use raw_input() instead, it's much safer! Convert the string to a float to get your number. Another good thing is to add a "divide by zero error" trap. All things to learn!
# took def out of the loop and added quit option
# also switched to raw_input and convert to float
# added divide by zero trap
print "the legable symbols are '+','-','/' and '*'"
print "it on a 10 loop so you can use it 10 times"
print
def plus():
print var_1, "+", var_3, "=", var_1 + var_3
def min():
print var_1, "-", var_3, "=", var_1 - var_3
def multi():
print var_1, "*", var_3, "=", var_1 * var_3
def div():
if var_3 == 0:
print "division by zero error"
else:
print var_1, "/", var_3, "=", (var_1 + 0.0) / var_3
loop = 0
while (loop < 10):
loop = loop + 1
try:
var_1 = float(raw_input ("Type first number (q to quit):"))
var_2 = raw_input ("Type Operation:")
var_3 = float(raw_input ("Type second number:"))
except:
# entering q would be an error so it goes here:
print "Thank you for using my calculator program!"
break
if var_2 == "+": plus()
if var_2 == "-": min()
if var_2 == "*": multi()
if var_2 == "/": div()
if var_2 == "q": break
print
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
thanks for that but for some reason all your code does is restarts the shell.
i cannot think why because i looked at it and it seems fine |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
Another tip is to avoid global variables when they're not needed. Make your operator functions more general:
def add(a, b): return a + b import operator two = operator.add(1, 1) # same as 1 + 1 from operator import add, sub, div, mul;
operator_map = { "+" : add, "-" : sub, "*" : mul, "/" : div }operator_map[val_2](val_1, val_3) operator_map[operator](number_1, number_2) |
|
|
|
|
|
#5 |
|
Hobbyist Programmer
|
Thanks for that. ive just started to learn C++ :p
|
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
A very good idea to learn C++ and Python. Python's strength is readability and program development speed. It isn't too difficult to develop your program concepts in Python and then translate them to C++ or C for the compiled product. A very nice skill to develop!
The only thing that will drive you nuts, are the repetitious and dogmatic type declarations that come with C++. Python, being an interpreter, figures those out on the fly. By the way, I happen to think that there are about 6 billion different types of people on this earth (presently).
__________________
I looked it up on the Intergnats! |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|