Python Forum
Beginner having Syntax Error problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner having Syntax Error problem
#1
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
Reply
#2
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

Reply
#3
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.
Reply
#4
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql Syntax error in pymysql ilknurg 4 350 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 442 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  Query Syntax Error hammer 2 415 Jan-03-2022, 02:30 PM
Last Post: hammer
Star I'm getting syntax error while using input function in def. yecktmpmbyrv 1 813 Oct-06-2021, 09:39 AM
Last Post: menator01
Exclamation Invalid syntax error(Predict Ethereum Price) lulu43366 2 1,262 Sep-24-2021, 01:24 PM
Last Post: lulu43366
  Line 42 syntax error..Help!!1 patpython 4 1,222 Sep-01-2021, 03:22 PM
Last Post: deanhystad
  Unexplained Invalid syntax Error cybertooth 5 1,358 Aug-02-2021, 10:05 AM
Last Post: cybertooth
  I have a syntax error! rohes_kaugummi 3 1,253 Jun-23-2021, 05:24 PM
Last Post: deanhystad
  import module with syntax error Skaperen 7 1,887 Jun-22-2021, 10:38 AM
Last Post: Skaperen
Heart near "(": syntax error Maryan 3 1,686 Jun-21-2021, 08:05 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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