![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Python calculator problem
I really just winged this one, no tutorial help, no nothing. Just took my knowldge and tried something. Chances are, its completly wrong. I had some trouble running it and i thought it might be just some mundane error but if it is more than just a typo of incorrect statement, I would like to know what it is. Thnx a million.
Its a, not-completed, calculator program ----> operator = raw_input ("Please enter the prefered operator: +, -, *, /")
if operator == '+' then:
print "Addition it is!
number_1 = input ("Please insert a number below 1,000,000:")
number_2 = input ("Please enter a second number below 1,000,000:")
print "The sum of the 2 numbers is:", number_1 + number_2
if operator == '-' then:
print "Subtraction it is!"
number_3 = input ("Please enter a number below 1,000,000:")
number_4 = input ("Please enter a number below 1,000,000:")
print "The sum of the 2 numbers is:" , number_3 - number_4
if operator == '*' then:
print "Multiplication it is!"
number_5 = input ("Please insert a number below 1,000,000:")
number_6 = input ("Please insert a number below 1,000,000:")
print "The sum of the 2 numers is:", number_5 * number_6
if operator == '/' then:
print "Division it is!"
number_7 = input ("Please insert a number below 1,000,000:")
number_8 = input ("Please insert a number below 1,000,000:")
print "The sum of the 2 numbers is:", number_7 / number_8The error message was: File "program.py", Line 2 if operator == '+' then: Invalid Syntax I'm assuming that means that "then" cant go their, or one of those statement is wrong. If so, how can i change it to make it work? Is their another way to put it without getting to advanced?
__________________
When will Jesus bring the porkchops? |
|
|
|
|
|
#2 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4
![]() |
Just one quick suggestion: instead of getting the operator, and getting user input for every operator, why not get the operator, get the input, and then apply it to the operator?
eg: get operator get input apply operator print result and the answer to your problem: the correct form is :
if condition:
#do stuff
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
@Jessehk
This is making my head hurt...Perhaps i'll just start over and take your advice.
__________________
When will Jesus bring the porkchops? |
|
|
|
|
|
#4 |
|
Expert Programmer
|
Nebula, you don't need the "then" keyword in Python, try removing it. :-D
So far its not a bad attempt, keep it up.
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#5 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,799
Rep Power: 5
![]() |
Also certain things can be made easier (and faster) for you:
number_7 = input ("Please insert a number below 1,000,000:")
number_8 = input ("Please insert a number below 1,000,000:")
print "The sum of the 2 numbers is:", number_7 / number_8Becomes print "The sum of the 2 numbers is:", input ("Please insert a number below 1,000,000:") / input ("Please insert a number below 1,000,000:")That's assuming you don't want to do anything with the variables after input. |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4
![]() |
And don't use input. It's bad because it runs on eval on what it receives from the command line, meaning people can execute arbitary Python code through your program.
Use int(raw_input("Your prompt here... ")). You may want to use a dictionary of operator functions to save you from coding a tonne of if statements, too. |
|
|
|
|
|
#7 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
|
|
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
I like coldDeath's answer. The first thing I would improve then is the input() function. To bad a screwy thing like that is hanging around an otherwise perfect language!
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
#9 |
|
Newbie
|
^^, yeah as said above i think you just need to take the Then out,
|
|
|
|
|
|
#10 |
|
Programmer
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3
![]() |
It might be better in this case to use functions.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|