Python Forum
Beginner having Syntax Error problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner having Syntax Error problem
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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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)

Possibly Related Threads…
Thread Author Replies Views Last Post
  Query Syntax Error hammer 2 202 Jan-03-2022, 02:30 PM
Last Post: hammer
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 540 Oct-06-2021, 09:39 AM
Last Post: menator01
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 685 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Line 42 syntax error..Help!!1 patpython 4 850 Sep-01-2021, 03:22 PM
Last Post: deanhystad
  Unexplained Invalid syntax Error cybertooth 5 897 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  I have a syntax error! rohes_kaugummi 3 982 Jun-23-2021, 05:24 PM
Last Post: deanhystad
  import module with syntax error Skaperen 7 1,271 Jun-22-2021, 10:38 AM
Last Post: Skaperen
Heart near "(": syntax error Maryan 3 1,328 Jun-21-2021, 08:05 AM
Last Post: ibreeden
Exclamation getting a syntax error and i don't know why ilikedofs 10 1,779 May-13-2021, 08:52 PM
Last Post: deanhystad
  Even number code syntax error MrCeez 1 917 May-02-2021, 06:43 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020