Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 29th, 2004, 09:36 AM
python_begina python_begina is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: england
Posts: 8 python_begina User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 53 sec
Reputation 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 11:58 AM. Reason: Code Tags again

Reply With Quote
  #2  
Old May 29th, 2004, 12:07 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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..

Quote:
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.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : May 29th, 2004 at 12:12 PM.

Reply With Quote
  #3  
Old May 29th, 2004, 05:22 PM
python_begina python_begina is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: england
Posts: 8 python_begina User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 53 sec
Reputation 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.

Reply With Quote
  #4  
Old May 29th, 2004, 05:28 PM
python_begina python_begina is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: england
Posts: 8 python_begina User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 53 sec
Reputation 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

Reply With Quote
  #5  
Old May 29th, 2004, 05:38 PM
NetBSD NetBSD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Canada
Posts: 234 NetBSD Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 9 h 25 m 35 sec
Reputation Power: 0
Send a message via MSN to NetBSD
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

Reply With Quote
  #6  
Old May 29th, 2004, 09:55 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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/l...exceptions.html

Have fun,

Mark.

Reply With Quote
  #7  
Old May 30th, 2004, 04:50 AM
python_begina python_begina is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: england
Posts: 8 python_begina User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 53 sec
Reputation Power: 0
thanks guys.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > rookie question about python


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT