Home › Hindi › SyntaxError: invalid syntax

Python SyntaxError: invalid syntax — Hindi में 8 कारण और उनका fix

🌐 यह page Hindi में है · Read this in English →

invalid syntaxunexpected EOFEOL while scanning string literal

पहला rule: galti अक्सर एक line ऊपर होती है

SyntaxError code चलने से पहले आता है — Python आपकी line पढ़ ही नहीं पाया, इसलिए program की एक भी line नहीं चलती (कोई output भी नहीं आता)।

सबसे काम की बात: Python galti वहाँ बताता है जहाँ उसे पता चली, वहाँ नहीं जहाँ वह हुई। अगर बताई गई line बिल्कुल सही दिख रही है, तो उससे ऊपर वाली line देखो — 80% बार अधूरा bracket या quote वहीं होता है।

1. Colon (:) भूल जाना

if marks > 40      # ❌ SyntaxError: expected ':'
    print("Pass")

if, elif, else, for, while, def, class, try, except, finally, with — इन सबकी line हमेशा : पर खत्म होती है।

2. Bracket बंद करना भूल जाना

print("Total:", total     # ❌ बंद नहीं हुआ
print("Done")             # error यहाँ दिखेगा!

Python अगली line को उसी statement का हिस्सा समझता रहता है, इसलिए error नीचे दिखता है। File के आखिर में यह SyntaxError: unexpected EOF while parsing बन जाता है — मतलब "file खत्म हो गई पर कुछ अधूरा रह गया"।

3. Quotes की गड़बड़ (EOL while scanning string literal)

naam = "Priya      # ❌ SyntaxError: EOL while scanning string literal
line = 'It's ok'   # ❌ बीच वाला ' string को जल्दी बंद कर देता है

line = "It's ok"   # ✅ बाहर double quotes
line = 'It\'s ok'  # ✅ या escape कर दो

"EOL" यानी end of line — Python line के आखिर तक पहुँच गया पर closing quote मिला ही नहीं।

4. = और == की उलझन

if marks = 40:       # ❌ SyntaxError: invalid syntax. Maybe you meant '==' ?
if marks == 40:      # ✅ तुलना के लिए ==
marks = 40           # ✅ value रखने के लिए =

= value रखता है, == तुलना करता है। Python 3.10+ यह suggestion खुद देता है।

5. Python 2 वाला print

print "Hello"      # ❌ Python 3 में SyntaxError
print("Hello")     # ✅

पुराने tutorials और किताबों से copy किया code अक्सर यही galti लाता है — Python 3 में print एक function है, इसलिए brackets ज़रूरी हैं।

6–8. बाकी तीन common cases

6. Keyword को variable बनाना: class = 10 या for = 5 — ❌। class, for, if, in, is, None, lambda Python के reserved words हैं। class_ या student_class लिखो।

7. Variable का नाम number से शुरू: 2nd_name = "x" ❌ → second_name ✅।

8. Comma भूल जाना:

nums = [1, 2 3]                    # ❌ 2 और 3 के बीच comma गायब
d = {"a": 1 "b": 2}                # ❌ यही galti dictionary में
print("Naam:" naam)                # ❌ print के arguments के बीच

SyntaxError ढूँढने का 4-step तरीका

1. Error वाली line और उससे एक ऊपर वाली — दोनों पढ़ो।
2. Brackets गिनो: ( जितने हैं उतने ही ) होने चाहिए। अच्छा editor matching bracket highlight करता है।
3. Colon check करो — block खोलने वाली हर line के आखिर में।
4. String के quotes देखो — जोड़ी में हैं या नहीं।

Syntax highlighting वाला editor (VS Code) इनमें से आधी galtiyan टाइप करते ही रंग बदलकर दिखा देता है — इसीलिए notepad में Python मत लिखो।

🛠️ Practice करो (free, browser में)

पढ़ना काफी नहीं — यही bug खुद fix करके देखो। नीचे के problems browser में ही चलते हैं, कोई install नहीं, कोई login नहीं।

सारे free problems देखो →

अक्सर पूछे जाने वाले सवाल

SyntaxError: invalid syntax क्यों आता है?

जब Python आपकी line को समझ ही नहीं पाता — आमतौर पर colon, bracket, quote या comma गायब होता है, या = की जगह == चाहिए होता है। यह error code चलने से पहले आता है, इसलिए program की एक भी line नहीं चलती।

error जिस line पर दिख रहा है वह तो सही है, फिर galti कहाँ है?

अक्सर galti उससे एक line ऊपर होती है — अधूरा bracket या quote अगली line तक चलता रहता है, इसलिए Python उसे अगली line पर report करता है। हमेशा दोनों lines देखें।

unexpected EOF while parsing का मतलब क्या है?

file खत्म हो गई पर कोई bracket, quote या block अधूरा रह गया। सबसे नीचे से ऊपर की तरफ brackets मिलाते जाएँ।

print "Hello" क्यों नहीं चलता?

Python 3 में print एक function है, इसलिए print("Hello") लिखना पड़ता है। बिना brackets वाला syntax Python 2 का है।

आगे पढ़ो