Beginner, Syntax Error

This is the place for queries that don't fit in any of the other categories.

Beginner, Syntax Error

Postby ObsidianFire99 » Fri Feb 15, 2013 11:52 am

I'm trying out my first python programs, but I've got an error I can't fix when trying a simple calculator. The code is:
Code: Select all
def mycalc():
    In1 = input('Input your first Number');
    In2 = input('Input your second Number');
    op = raw_input('Input your desired Operation');
    if op == '+':
            answer = In1 + In2
    elif op == '-':
            answer = In1 - In2
    else:
            answer = 'Please enter correct values. Use only + or -'

    print ('answer')

When I try it out in the shell, I can enter values which are then printed out for me but cannot enter an operation - I get syntax error. What am I doing wrong and how can I fix it?
ObsidianFire99
 
Posts: 2
Joined: Fri Feb 15, 2013 11:45 am

Re: Beginner, Syntax Error

Postby stranac » Fri Feb 15, 2013 1:12 pm

You should never use input() on python 2(because it evaluates user input as python code).
You should use raw_input() instead, and do the type conversion yourself.
Code: Select all
in1 = float(raw_input('Input your first Number'))
in2 = float(raw_input('Input your second Number'))

But since you're already using raw_input() to get the operator, this shouldn't throw a SyntaxError.
I actually tested your code, and it works fine for me(except you should be printing answer, not 'answer').

Also, there are some style issues with your code:
  • you should use lowercase names for your variables
  • you should always indent with 4 spaces
  • don't use parens with print on python 2
  • there should be no space between the function name and the opening paren
  • you don't need the semicolons
For more style guides, check out PEP8.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: Beginner, Syntax Error

Postby sheffieldlad » Fri Feb 15, 2013 1:16 pm

Works ok for me but you need to change:

Code: Select all
print ('answer')


to

Code: Select all
print answer


otherwise it just prints the word answer :)
Python 2.7
Windows XP
sheffieldlad
 
Posts: 37
Joined: Sat Feb 09, 2013 3:03 pm
Location: UK

Re: Beginner, Syntax Error

Postby sheffieldlad » Fri Feb 15, 2013 1:17 pm

pipped to the post :)
Python 2.7
Windows XP
sheffieldlad
 
Posts: 37
Joined: Sat Feb 09, 2013 3:03 pm
Location: UK

Re: Beginner, Syntax Error

Postby ObsidianFire99 » Fri Feb 15, 2013 2:49 pm

Ok, thanks, I made your changes but...
When I hit F5 in IDLE it's not printing the 'Input your first number' part at all. It just prints a few blank lines and then does nothing.
ObsidianFire99
 
Posts: 2
Joined: Fri Feb 15, 2013 11:45 am

Re: Beginner, Syntax Error

Postby sheffieldlad » Fri Feb 15, 2013 3:14 pm

Thats because you have you code in a function but nothing is calling the function.
remove 1st line of your code or add code to call the function. like this:

Code: Select all
mycalc();


If you decide to remove the first line you'll have to unindent the rest of your code by 4 spaces.

Do a google search for calling function in python.
At the moment none of your code is being run when you press F5.
Python 2.7
Windows XP
sheffieldlad
 
Posts: 37
Joined: Sat Feb 09, 2013 3:03 pm
Location: UK

Re: Beginner, Syntax Error

Postby stranac » Fri Feb 15, 2013 5:31 pm

sheffieldlad wrote:
Code: Select all
mycalc();

No need for the semicolon.
A new line ends a statement in python.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1092
Joined: Thu Feb 07, 2013 3:42 pm

Re: Beginner, Syntax Error

Postby sheffieldlad » Fri Feb 15, 2013 6:19 pm

I stand corrected :)
Python 2.7
Windows XP
sheffieldlad
 
Posts: 37
Joined: Sat Feb 09, 2013 3:03 pm
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests