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

New Free Tools on Dev Shed!

#1
May 29th, 2004, 10:36 AM
 python_begina
Registered User

Join Date: May 2004
Location: england
Posts: 8
Time spent in forums: 29 m 53 sec
Reputation Power: 0

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
elif function == m:
m = number1 - number2
elif function == t:
t = number1 * number2
elif function == d:
d = number1 / number2

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
May 29th, 2004, 01:07 PM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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 01:12 PM.

#3
May 29th, 2004, 06:22 PM
 python_begina
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.

#4
May 29th, 2004, 06:28 PM
 python_begina
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

#5
May 29th, 2004, 06:38 PM
 NetBSD
Contributing User

Join Date: Apr 2004
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 == '+':
elif function == '-':
elif function == '*':
elif function == '/':

#6
May 29th, 2004, 10:55 PM
 netytan
Hello World :)

Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
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:

if function == 'a':
elif function == 'b':
elif function == 'm':
elif function == 'd':
try:
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)'

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.

#7
May 30th, 2004, 05:50 AM
 python_begina
Registered User

Join Date: May 2004
Location: england
Posts: 8
Time spent in forums: 29 m 53 sec
Reputation Power: 0
thanks guys.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > rookie question about python