Home › Hindi › TypeError कैसे fix करें

Python TypeError कैसे fix करें — Hindi में पूरा guide

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

6 सबसे common TypeError messagesहर message का exact fixFree browser practice

TypeError होता क्या है?

TypeError का मतलब है: आपने किसी चीज़ पर ऐसा काम किया जो उसके type पर होता ही नहीं। जैसे text को number में जोड़ना, list को call करना, या number पर loop चलाना। Python कभी अपने मन से guess करके type नहीं बदलता — इसे "strong typing" कहते हैं, और यही आपको चुपचाप गलत result मिलने से बचाता है।

TypeError का fix ढूँढने का सबसे तेज़ तरीका: crash वाली line के ठीक ऊपर print(type(x), x) लगा दो। 90% बार वहीं दिख जाएगा कि जिसे आप number समझ रहे थे वह असल में <class 'str'> है या None है।

1. TypeError: can only concatenate str (not "int") to str

सबसे common case — + से text और number जोड़ना।

score = 90
print("Score: " + score)     # ❌ TypeError

print(f"Score: {score}")     # ✅ सबसे साफ तरीका
print("Score:", score)       # ✅ comma से print खुद space लगा देता है
print("Score: " + str(score))  # ✅ manual conversion

Rule: + का मतलब strings के बीच "जोड़ो" और numbers के बीच "plus" है — दोनों mix नहीं होते। f-string सबसे अच्छा fix है क्योंकि वह conversion खुद करता है।

2. TypeError: unsupported operand type(s) for +: 'int' and 'str'

यह ऊपर वाले का उल्टा है — पहले number है, बाद में string। सबसे बड़ा source: input()। input() हमेशा string देता है, चाहे user ने digits ही क्यों न टाइप किए हों।

umar = input("Umar: ")   # user ने लिखा 25 → umar = "25" (string!)
print(umar + 1)          # ❌ TypeError

umar = int(input("Umar: "))   # ✅ पहले number बनाओ
print(umar + 1)               # 26

इसी तरह JSON/CSV से आया data भी अक्सर string होता है — calculation से पहले int() या float() लगाओ।

3. TypeError: 'int' object is not callable

"not callable" = आपने किसी चीज़ के आगे () लगाए, पर वह function है ही नहीं। सबसे आम वजह: built-in function के नाम पर variable बना देना।

nums = [1, 2, 3]
sum = sum(nums)      # यहाँ sum अब एक int (6) है, function नहीं
total = sum(nums)    # ❌ TypeError: 'int' object is not callable

Fix: variables के नाम कभी sum, list, str, len, max, type, id, input मत रखो। ऊपर वाले case में total = sum(nums) लिखो।

4. TypeError: 'int' object is not iterable

"not iterable" = इस चीज़ पर loop नहीं चल सकता। Numbers पर loop नहीं चलता, ranges और lists पर चलता है।

for i in 5:            # ❌ TypeError: 'int' object is not iterable
    print(i)

for i in range(5):     # ✅ 0 1 2 3 4
    print(i)

इसका भाई error है TypeError: 'NoneType' object is not iterable — वह तब आता है जब कोई function return करना भूल गया और आप उसके result पर loop चला रहे हो।

5. TypeError: 'NoneType' object is not subscriptable

इसका मतलब: जिस चीज़ पर आपने [0] लगाया वह None है। Python में कई list methods जगह पर ही बदलाव करती हैं और None लौटाती हैं — sort(), append(), reverse()।

nums = [3, 1, 2]
nums = nums.sort()     # sort() None लौटाता है!
print(nums[0])         # ❌ TypeError: 'NoneType' object is not subscriptable

nums = [3, 1, 2]
nums.sort()            # ✅ nums खुद sorted हो गई
print(nums[0])         # 1
# या: sorted_nums = sorted(nums)  → नई list लौटाता है

Rule याद रखो: list.sort() जगह पर sort करता है (None देता है), sorted(list) नई list देता है।

6. TypeError: func() missing 1 required positional argument

Function को जितने arguments चाहिए उतने नहीं दिए — या class में self की उलझन है।

def add(a, b):
    return a + b

add(5)        # ❌ TypeError: add() missing 1 required positional argument: 'b'
add(5, 3)     # ✅ 8

def add(a, b=0):   # ✅ default value दे दो तो एक argument भी चलेगा
    return a + b

उल्टा message takes 2 positional arguments but 3 were given तब आता है जब class की method में self लिखना भूल गए हों।

Debug करने का 3-step तरीका

Step 1: traceback की आखिरी line पढ़ो — वहाँ दोनों types लिखे होते हैं ('int' and 'str')। यही बता देता है कि कौन सी चीज़ गलत type की है।

Step 2: crash वाली line से ठीक ऊपर print(type(x), type(y)) लगाओ और चलाओ।

Step 3: जो value गलत type की है उसे source पर ठीक करो (जहाँ वह बनी है), crash वाली line पर पट्टी मत बाँधो। input() के result को उसी line पर int() कर देना सबसे साफ fix है।

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

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

सारे free problems देखो →

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

Python में TypeError का मतलब क्या है?

TypeError का मतलब है कि किसी value पर ऐसा operation किया गया जो उसके type पर support नहीं करता — जैसे string और int को + से जोड़ना, या किसी number पर for loop चलाना।

can only concatenate str (not "int") to str कैसे fix करें?

number को string बनाइए या f-string इस्तेमाल कीजिए: print(f"Score: {score}") या print("Score: " + str(score))। print में comma लगाना भी काम करता है।

input() से लिए number पर TypeError क्यों आता है?

क्योंकि input() हमेशा string लौटाता है। calculation से पहले int(input(...)) या float(input(...)) लिखकर उसे number में बदलें।

'int' object is not callable क्यों आता है?

आपने किसी variable के आगे brackets लगाए हैं जो function नहीं है। अक्सर वजह यह होती है कि sum, list या len जैसे built-in नाम पर variable बना दिया गया है — variable का नाम बदल दें।

आगे पढ़ो