Python Forum

Full Version: Beginner having Syntax Error problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys I am new to Python and I have overcome a problem.Can anybody help?
I am trying to make a simple calculator. Big Grin Big Grin

This is the code.

x=int(input('Input first number.\n'));
y=int(input('Input second number.\n'));
a=int(input('What calculation you want to perform?\n 1 +, 2 -,3 *,4 / \n'));
If a==1;print('The answer is ',x+y);
If a==2;print('The answer is ',x-y);
If a==3;print('The answer is ',x*y);
If a==4;print('The answer is ',x/y);
And the error is SyntaxError: invalid syntax which is pointing to the "a" in line 4 of this programme. Wall Wall
there are number of issues in your code
  • it is if, not If
  • it should be :, not ;, e.g. if a==1:print('The answer is ',x+y)
  • no need of ; at the end of line
  • one-line if are generally discouraged
1. there is no need of; at the end of the line in while coding in python.
2. write "if" instead of "If", remember its a programing language not English.
3. Try to re-structure your if statements. one line code is often disregarded.
x=int(input('Input first number.\n'))
y=int(input('Input second number.\n'))
a=int(input('What calculation you want to perform?\n 1 +, 2 -,3 *,4 / \n'))
if a==1:print('The answer is ',x+y)
if a==2:print('The answer is ',x-y)
if a==3:print('The answer is ',x*y)
if a==4:print('The answer is ',x/y)