| | |
Python Beginner Help (Calculator)
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2008
Posts: 26
Reputation:
Solved Threads: 1
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!
Thanks,
Inkcoder
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/div2Thanks,
Inkcoder
•
•
Join Date: Jun 2008
Posts: 127
Reputation:
Solved Threads: 31
THe line:
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)
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)
op=raw_input(">")
Last edited by slate; Jun 7th, 2008 at 7:40 am.
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:
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)
print "At the >> prompt enter x op y" print "where x and y are numbers and op is" print "- + / * ** // for instance 12 * 9" result = input(">> ") print "result =", result
Last edited by sneekula; Jun 7th, 2008 at 11:23 am.
No one died when Clinton lied.
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Assembly or C++ for a Beginner? (IT Professionals' Lounge)
Other Threads in the Python Forum
- Previous Thread: Check if a number is a prime number (Python)
- Next Thread: Overlaying graphics in Python?
| Thread Tools | Search this Thread |
alarm assignment avogadro beginner bluetooth character cmd code customdialog cx-freeze data decimals dictionary directory dynamic error examples exe file float format function generator getvalue gnu graphics gui halp homework http ideas import input ip itunes java leftmouse line linux list lists logging loop maze millimeter module mouse number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython xlib






