943,800 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1117
  • Python RSS
Jun 7th, 2008
0

Python Beginner Help (Calculator)

Expand Post »
Hello Everyone,

I just joined here hoping to increase my knowledge at python. I have been beginning to learn this language as of yesterday. I have decided that if I really want to increase my knowledge I should start on a project. Well here is my first project, a simple calculator. But for whatever reason when I run the program I'm getting some strange errors... Whatever help and advice you guys could give me would be most appreciated!

#Calculator Test1

print "Calculator:"
print "1)+" 
print "2)-" 
print "3)*" 
print "4)/" 
print " "
print "Select Choice:"
op = input(">")
print " "

#Add
if op == "1":
    print "a + b"
    add1 = input("a:")
    add2 = input("b:")
    print "a + b =", add1+add2

#Sub
if op == "2":
    print "a - b"
    sub1 = input("a:")
    sub2 = input("b:")
    print "a - b =", sub1-sub2

#Multi
if op == "3":
    print "a * b"
    mul1 = input("a:")
    mul2 = input("b:")
    print "a * b =", mul1*mul2

#Div
if op == "4":
    print "a / b"
    div1 = input("a:")
    div2 = input("b:")
    print "a / b =", div1/div2

Thanks,
Inkcoder
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
inkcoder is offline Offline
26 posts
since Jun 2008
Jun 7th, 2008
1

Re: Python Beginner Help (Calculator)

THe line:

python Syntax (Toggle Plain Text)
  1. op = input(">")

Takes the input AND evaluates it. That means inputing "1" results in eval("1") which is int("1').
Putting in something like "a" results in the interpreter looking for a name "a" and not finding it.

For the beginning I recommend to use:

python Syntax (Toggle Plain Text)
  1. op=raw_input(">")
Last edited by slate; Jun 7th, 2008 at 7:40 am.
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
Jun 7th, 2008
0

Re: Python Beginner Help (Calculator)

First, welcome to the forum!

What 'slate' is really telling you is that input() is for numbers and raw_input() is for strings. Using input() has certain risks since you can also enter a command and it will excute it.

Anyway, if you want to use
op = input(">")
then op will not be a string
and you have to use this if statement
if op == 1:

In the matter of fact, input() allows you to create this very simple calculator, since it evaluates mathematical expressions directly:
python Syntax (Toggle Plain Text)
  1. print "At the >> prompt enter x op y"
  2. print "where x and y are numbers and op is"
  3. print "- + / * ** // for instance 12 * 9"
  4. result = input(">> ")
  5. print "result =", result
Last edited by sneekula; Jun 7th, 2008 at 11:23 am.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Eratosthenes sieve
Next Thread in Python Forum Timeline: Overlaying graphics in Python?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC