Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Nov 29th, 2005, 9:57 AM   #1
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
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
HaCkeR is offline   Reply With Quote
Old Nov 29th, 2005, 11:19 AM   #2
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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!
Dietrich is offline   Reply With Quote
Old Nov 29th, 2005, 11:29 AM   #3
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
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
HaCkeR is offline   Reply With Quote
Old Nov 29th, 2005, 11:45 AM   #4
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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
In fact, you don't even have to do this, since python provides the operator module:
import operator
two = operator.add(1, 1)   # same as 1 + 1
A dictionary could also be used in place of if statements. You could create a dictionary that associates an operator string (such as "+") with an operator function (such as operator.add):
from operator import add, sub, div, mul;
operator_map = { "+" : add, "-" : sub, "*" : mul, "/" : div }
You can then use operator_map to find the correct function and to execute it:
operator_map[val_2](val_1, val_3)
However, it might also be a good idea to name your variables something a little more descriptive than val_1 to val_3. Maybe: number_1, operator, number_2:
operator_map[operator](number_1, number_2)
Arevos is offline   Reply With Quote
Old Nov 29th, 2005, 12:46 PM   #5
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
Thanks for that. ive just started to learn C++ :p
HaCkeR is offline   Reply With Quote
Old Nov 30th, 2005, 1:32 PM   #6
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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!
Dietrich 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 4:40 PM.

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