Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 17th, 2006, 11:20 PM   #1
Nebula
Hobbyist Programmer
 
Nebula's Avatar
 
Join Date: Oct 2005
Posts: 193
Rep Power: 3 Nebula is on a distinguished road
Send a message via AIM to Nebula
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_8
Thnx again fellas! :banana:

The 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?
Nebula is offline   Reply With Quote
Old Jan 17th, 2006, 11:24 PM   #2
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 630
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Jan 17th, 2006, 11:31 PM   #3
Nebula
Hobbyist Programmer
 
Nebula's Avatar
 
Join Date: Oct 2005
Posts: 193
Rep Power: 3 Nebula is on a distinguished road
Send a message via AIM to Nebula
@Jessehk
This is making my head hurt...Perhaps i'll just start over and take your advice.
__________________
When will Jesus bring the porkchops?
Nebula is offline   Reply With Quote
Old Jan 18th, 2006, 1:29 AM   #4
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
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.
coldDeath is offline   Reply With Quote
Old Jan 18th, 2006, 1:08 PM   #5
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Posts: 1,799
Rep Power: 5 Sane will become famous soon enough
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_8

Becomes
	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.
Sane is offline   Reply With Quote
Old Jan 18th, 2006, 4:02 PM   #6
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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.
Cerulean is offline   Reply With Quote
Old Jan 18th, 2006, 6:48 PM   #7
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by Cerulean
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.
I wouldn't teach someone to do that until they were very comfortable with the language. It's just confusing.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Jan 19th, 2006, 1:17 PM   #8
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

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!
Dietrich is offline   Reply With Quote
Old Feb 7th, 2006, 5:02 PM   #9
Namingishard
Newbie
 
Join Date: Feb 2006
Location: TX
Posts: 23
Rep Power: 0 Namingishard is on a distinguished road
Send a message via AIM to Namingishard Send a message via MSN to Namingishard
^^, yeah as said above i think you just need to take the Then out,
Namingishard is offline   Reply With Quote
Old Feb 7th, 2006, 5:10 PM   #10
snipertomcat
Programmer
 
snipertomcat's Avatar
 
Join Date: Nov 2005
Location: Spring Valley, CA
Posts: 52
Rep Power: 3 snipertomcat is on a distinguished road
It might be better in this case to use functions.
snipertomcat is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:09 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC