Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
syntax error
#1
I am writing this peace of code in Pycharm but it gives me a invalid syntax error can anyone tell me what i'am doing wrong


one = int(raw_input("Enter a number between 1 to 10 : "))
two = int(raw_input("Enter a number between 1 to 10 : "))
if (one >= 0) and (one <= 10):
    print ("You secret number is : ",one * two)
    else:
    print ("incorect second value please enter between 1 to 10")
else:
    print ("incorrect first value")

Error:

C:\Users\HON\PycharmProjects\input\venv\Scripts\python.exe C:/Users/HOK/PycharmProjects/input/as.py
  File "C:/Users/HON/PycharmProjects/input/as.py", line 5
    else:
       ^
SyntaxError: invalid syntax

Process finished with exit code 1
Reply
#2
else needs to be indented to the same level as the if statement it belongs to.

An if statement can only have ONE else statement. If you need to check several conditions and apply different outcomes, you can use elif.

As you are learning Python, I strongly recommend you use Python 3 rather than legacy Python. (Support for Python 2 ends on 1st January 2020.) Generally, I'd suggest that only experienced programmers responsible for maintaining old code, or those with a absolute dependency on a library that has not yet been updated and for which there are no Python 3 compatible alternatives should be using Python 2. Python 3 has many advances, fixed a lot of issues, and is more performant.

Example using if, elif, else:

num = 5  # test data for example purposes
if 1 <= num <= 10:
    print(f'Num {num} is between 1 and 10')
elif num < 1:
    print(f'Num {num} is less than 1')
else:
    print(f'Num {num} is greater than 10')
I am trying to help you, really, even if it doesn't always seem that way
Reply
#3
Quote:As you are learning Python, I strongly recommend you use Python 3 rather than legacy Python. (Support for Python 2 ends on 1st January 2020.) Generally, I'd suggest that only experienced programmers responsible for maintaining old code, or those with a absolute dependency on a library that has not yet been updated and for which there are no Python 3 compatible alternatives should be using Python 2. Python 3 has many advances, fixed a lot of issues, and is more performant.

Thank you and from now on i will start with python 3
Reply
#4
(Sep-29-2018, 11:57 AM)FIVE9 Wrote:
Quote:As you are learning Python, I strongly recommend you use Python 3 rather than legacy Python. (Support for Python 2 ends on 1st January 2020.) Generally, I'd suggest that only experienced programmers responsible for maintaining old code, or those with a absolute dependency on a library that has not yet been updated and for which there are no Python 3 compatible alternatives should be using Python 2. Python 3 has many advances, fixed a lot of issues, and is more performant.

Thank you and from now on i will start with python 3
Great. In that case, you should know that Python 2's raw_input was renamed input in Python 3 (and the original input was dropped from the language).
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
Thank you @gruntfutuk i will keep that in mind
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Syntax error for "root = Tk()" dlwaddel 15 1,156 Jan-29-2024, 12:07 AM
Last Post: dlwaddel
Photo SYNTAX ERROR Yannko 3 373 Jan-19-2024, 01:20 PM
Last Post: rob101
  Syntax error while executing the Python code in Linux DivAsh 8 1,542 Jul-19-2023, 06:27 PM
Last Post: Lahearle
  Code is returning the incorrect values. syntax error 007sonic 6 1,206 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  syntax error question - string mgallotti 5 1,290 Feb-03-2023, 05:10 PM
Last Post: mgallotti
  Syntax error? I don't see it KenHorse 4 1,241 Jan-15-2023, 07:49 PM
Last Post: Gribouillis
  Syntax error tibbj001 2 881 Dec-05-2022, 06:38 PM
Last Post: deanhystad
  Python-for-Android:p4a: syntax error in main.py while compiling apk jttolleson 2 1,828 Sep-17-2022, 04:09 AM
Last Post: jttolleson
  Mysql Syntax error in pymysql ilknurg 4 2,344 May-18-2022, 06:50 AM
Last Post: ibreeden
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 1,950 Feb-21-2022, 08:58 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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