The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
rookie question about python
Discuss rookie question about python in the Python Programming forum on Dev Shed. rookie question about python Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

May 29th, 2004, 09:36 AM
|
|
Registered User
|
|
Join Date: May 2004
Location: england
Posts: 8
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
|

May 29th, 2004, 12:07 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

May 29th, 2004, 05:22 PM
|
|
Registered User
|
|
Join Date: May 2004
Location: england
Posts: 8
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. 
|

May 29th, 2004, 05:28 PM
|
|
Registered User
|
|
Join Date: May 2004
Location: england
Posts: 8
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
|

May 29th, 2004, 05:38 PM
|
|
Contributing User
|
|
Join Date: Apr 2004
Location: Canada
Posts: 242
Time spent in forums: 11 h 3 m 17 sec
Reputation 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
|

May 29th, 2004, 09:55 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

May 30th, 2004, 04:50 AM
|
|
Registered User
|
|
Join Date: May 2004
Location: england
Posts: 8
Time spent in forums: 29 m 53 sec
Reputation Power: 0
|
|
|
thanks guys.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|