Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 46

Calculator Program

हम क्या बना रहे हैं

Calculator दो numbers और एक operator (+, -, *, /) लेकर result दिखाता है। यह बढ़िया पहला project है जो input, conditions, functions और error handling एक साथ लाता है। हम इसे चरण-दर-चरण बनाएँगे और divide by zero से सुरक्षित करेंगे।

💡 याद रखिए: input को float() से बदलिए, वरना "2" + "3" 5 के बजाय "23" देता है।

एक बुनियादी calculator

a = float(input("First number: "))
op = input("Operator (+ - * /): ")
b = float(input("Second number: "))

if op == "+":
    print(a + b)
elif op == "-":
    print(a - b)
elif op == "*":
    print(a * b)
elif op == "/":
    print(a / b)
else:
    print("Invalid operator")
First number: 10
Operator: +
Second number: 5
15.0

हम दो numbers (float में बदले) और एक operator पढ़ते हैं, फिर मेल खाता operation चलाने को if-elif इस्तेमाल करते हैं। else invalid operator पकड़ता है।

Functions इस्तेमाल

हर operation को function में रखना code साफ़ और दोबारा-इस्तेमाल-योग्य रखता है।

def calculate(a, b, op):
    if op == "+": return a + b
    if op == "-": return a - b
    if op == "*": return a * b
    if op == "/": return a / b
    return "Invalid operator"

print(calculate(20, 4, "/"))
5.0

अब logic एक calculate function में रहती है जिसे कहीं भी call कर सकते हैं, जो हर जगह if-elif दोहराने से साफ़ है।

Divide by zero संभालना

def calculate(a, b, op):
    if op == "/":
        if b == 0:
            return "Cannot divide by zero"
        return a / b
    # ... बाक़ी operators

print(calculate(10, 0, "/"))
Cannot divide by zero

शून्य से भाग program crash कर देता, तो हम पहले if b == 0 जाँचते हैं। आप try-except ZeroDivisionError block भी इस्तेमाल कर सकते हैं।

User को quit चुनने तक कई calculations करने दीजिए।

while True:
    a = float(input("First number: "))
    op = input("Operator: ")
    b = float(input("Second number: "))
    print("Result:", calculate(a, b, op))

    again = input("Another? (y/n): ")
    if again != "y":
        break
print("Goodbye")

while True loop calculator दोहराता है। हर result के बाद हम पूछते हैं user और चाहता है या नहीं; "y" के अलावा कुछ भी type करना loop से break करके ख़त्म कर देता है।

आम गलतियाँ

  • Input न बदलना। input() string देता है — गणित को float() या int() इस्तेमाल कीजिए।
  • Divide by zero अनदेखा करना। भाग को हमेशा जाँच या try-except से guard कीजिए।
  • बुरे operators के लिए else नहीं। Invalid input संभालिए ताकि program चुपचाप fail न हो।
  • = से तुलना। operator तुलना को == इस्तेमाल कीजिए, assignment = नहीं।

सारांश

  • Calculator दो numbers और एक operator पढ़कर गणना करता है।
  • गणित से पहले input को float() से बदलिए।
  • Operation चुनने को if-elif या calculate function इस्तेमाल कीजिए।
  • Divide by zero से जाँच या try-except से guard कीजिए।
  • User को दोहराने देने को while loop जोड़िए।

आगे, फ़ैसले सीखिए if-else in Python में, या code दोबारा इस्तेमाल कीजिए Python functions से।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Python में number input कैसे लूँ?
text पढ़ने को input() इस्तेमाल कीजिए, फिर उसे float() या int() से बदलिए — जैसे a = float(input("Enter a number: "))। बदले बिना input string रहता है और गणित काम नहीं करेगा।
Python में divide by zero कैसे संभालूँ?
भाग से पहले जाँचिए divisor शून्य तो नहीं, या भाग को ZeroDivisionError के लिए try-except में लपेटिए। program crash होने देने के बजाय "cannot divide by zero" जैसा message दिखाइए।
Calculator के लिए if-elif या functions इस्तेमाल करूँ?
दोनों चलते हैं। पहले version को if-elif ठीक है। हर operation को function में रखना code साफ़ और दोबारा-इस्तेमाल-योग्य बनाता है, और programs बढ़ने पर अच्छी आदत है।
Calculator को दोहराने वाला कैसे बनाऊँ?
इसे while True loop में लपेटिए और quit करने का विकल्प दीजिए। हर calculation के बाद पूछिए user और चाहता है या नहीं, और exit चुनने पर loop से break कीजिए।
मेरा calculator numbers जोड़ने के बजाय जोड़कर चिपका क्यों देता है?
यह तब होता है जब inputs अब भी strings हों — "2" + "3" "23" देता है। पहले inputs को float या int में बदलिए ताकि Python असली गणित करे।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicReverse String or List आपकी प्रगति: 46 / 66 (70%) · Lesson 47 / 66 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें