#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    england
    Posts
    8
    Rep Power
    0

    rookie question about python


    hi guys, im a relative beginner to the world of programming, i have done some very basic C++ programming before at college, but i decided to download python to learn some more about programming.
    i have two questions-
    the first:
    im reading a few turtorials, but im stuck when it comes to making a calculator. i know it sounds really bad, but i just cant get it to print the answer. here is what ive done:

    Code:
    function = raw_input("a,m,t,d   ")
    number1 = input("type a number here: ")
    number2 = input("type a number here: ")
    
    a = number1 + number2
    m = number1 - number2
    t = number1 * number2
    d = number1 / number2
    if function == a:
            a = number1 + number2
            answer = a
            print "the answer is", answer
    elif function == m:
            m = number1 - number2
            answer = m
            print answer
    elif function == t:
            t = number1 * number2
            answer = t
            print answer
    elif function == d:
            d = number1 / number2
            answer = d
            print answer
    could anyone help with this?

    my second question is:
    should i just give up and leave programming for the clever people, because i must admit , i have a very average inteligence level (IQ 95), and i must admit i find progamming pretty challenging. i am extremly interested in it though

    thanks all
    Last edited by netytan; May 29th, 2004 at 12:58 PM. Reason: Code Tags again
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The problem is that your compating function (which is a string returned by raw_input()) to the answer resulting from four numeric expressions. As a result none of your if or elif statments are being executed. All you need to do here is to check for the strings 'a', 'm', 't' and 'd' instead i.e.

    Code:
    #!/usr/bin/env python
    
    do = raw_input('a, b or c?')
    
    if do == 'a':
        print 'you pressed a'
    elif do == 'b':
        print 'you pressed b'
    elif do == 'c':
        print 'you pressed c'
    else:
        print 'I said, a, b or c not', do
    You don't need to evaluate the same expersion more than once in your program, so you can loose these lines..

    a = number1 + number2
    m = number1 - number2
    t = number1 * number2
    d = number1 / number2
    you might also want to try using int(raw_input()) instead of input() in future since input() can be used to execute arbitary expessions - so in the right surcumstances, a big security risk

    Mmmm, there answer to your second Q is even simpler. If you are interested in programming then program. Dont worry about not being as smart as the people around you since you'll never get anything done that way. Programming is an art, and as with all arts, takes a little time . Give yourself a break! Have a KitKat

    Good luck and for god sake have fun ,

    Mark.

    P.S. If you'd like to see the rewriten version of your program at all just ask.
    Last edited by netytan; May 29th, 2004 at 01:12 PM.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    england
    Posts
    8
    Rep Power
    0

    thanks


    thanks for the reply, i see where im going wrong now. im gonna keep at it until i get better and borrow a book from the library.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    england
    Posts
    8
    Rep Power
    0
    oh and yes please, if thats ok, i would like a copy of the program re-written correctly, just make sure i fully understand it. thanks
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Canada
    Posts
    242
    Rep Power
    0
    Code:
    function = raw_input("+, -, * or /: ")
    number1 = int(raw_input("first number: "))
    number2 = int(raw_input("second number: "))
    
    if function == '+':
            answer = number1 + number2
    elif function == '-':
            answer = number1 - number2
    elif function == '*':
            answer = number1 * number2
    elif function == '/':
            answer = number1 / number2
    
    print "the answer is ", answer
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Code:
    #!/usr/bin/env python
    
    while True:
        function = raw_input('(a, s, m, d or q) --')
    
        try:
            number1 = int(raw_input("first number: "))
            number2 = int(raw_input("second number: "))
        except ValueError:
            'Please enter a number...try again!'
    
        if function == 'a':
            answer = number1 + number2
        elif function == 'b':
            answer = number1 - number2
        elif function == 'm':
            answer = number1 * number2
        elif function == 'd':
            try:
                answer = number1 / number2
            except ZeroDivisionError:
                print 'Can't divide by Zero...try again!'
        elif function == 'q':
            break
        else:
            'You must enter either (a, s, m, d or q)'
    
        print "the answer is ", answer
    This should be pretty self explanitory if you have any experiance with error catching. Basically all thats happening here is were catching any forseeable errors. I've also added a while loop so that the program will keep asking you for untill you enter 'q' .

    You mgiht find this interesting when dealing with errors.

    http://www.python.org/doc/current/li...xceptions.html

    Have fun,

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    england
    Posts
    8
    Rep Power
    0
    thanks guys.

IMN logo majestic logo threadwatch logo seochat tools logo