![]() |
syntax error - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: syntax error (/thread-13133.html) |
syntax error - FIVE9 - Sep-29-2018 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 RE: syntax error - gruntfutuk - Sep-29-2018 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') RE: syntax error - FIVE9 - Sep-29-2018 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 RE: syntax error - gruntfutuk - Sep-29-2018 (Sep-29-2018, 11:57 AM)FIVE9 Wrote:Great. In that case, you should know that Python 2'sQuote: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. raw_input was renamed input in Python 3 (and the original input was dropped from the language).
RE: syntax error - FIVE9 - Sep-29-2018 Thank you @gruntfutuk i will keep that in mind |