DAY 01 — 60 DAY PYTHON CHALLENGE
💻 Python Calculator
Python में Simple Calculator बनाएं — हिंदी में
Advertisement
Complete Code
python — day01.py
def calculator(): print("=== 🧮 Python Calculator ===") print("1. जोड़ (+)") print("2. घटाव (-)") print("3. गुणा (*)") print("4. भाग (/)") try: choice = int(input("\nOperation चुनें (1-4): ")) a = float(input("पहला number: ")) b = float(input("दूसरा number: ")) if choice == 1: print(f"\n✅ {a} + {b} = {a+b}") elif choice == 2: print(f"\n✅ {a} - {b} = {a-b}") elif choice == 3: print(f"\n✅ {a} × {b} = {a*b}") elif choice == 4: if b == 0: print("❌ 0 से भाग नहीं होता!") else: print(f"\n✅ {a} ÷ {b} = {a/b:.2f}") else: print("❌ गलत choice!") except ValueError: print("❌ सही number डालें!") calculator()
OUTPUT
=== 🧮 Python Calculator ===
1. जोड़ (+)
2. घटाव (-)
3. गुणा (*)
4. भाग (/)
Operation चुनें (1-4): 1
पहला number: 15
दूसरा number: 5
✅ 15.0 + 5.0 = 20.0
यह Code कैसे काम करता है?
इस calculator में functions, try-except, और if-elif-else use किया। float() से decimal numbers भी handle होते हैं। ZeroDivisionError को check करना जरूरी है।
Advertisement
📋 Project ka Introduction
Calculator Python ka sabse pehla aur sabse popular beginner project hai. Agar aap Python seekhna shuru kar rahe hain, to Calculator banana aapke liye ek perfect starting point hai. Is project mein aap Python ki basic input/output, conditions (if-elif-else), aur arithmetic operations ko ek saath practice kar sakte hain.
Calculator project mein aap user se numbers aur operation lenge, calculation karenge, aur result dikhayenge. Yeh concept real duniya mein har jagah use hota hai — mobile apps, websites, aur computer software mein.
Is project ko banane ke baad aap confidently Python mein koi bhi mathematical program likh sakenge. Chaliye step-by-step samajhte hain ki yeh code kaise kaam karta hai!
Calculator project mein aap user se numbers aur operation lenge, calculation karenge, aur result dikhayenge. Yeh concept real duniya mein har jagah use hota hai — mobile apps, websites, aur computer software mein.
Is project ko banane ke baad aap confidently Python mein koi bhi mathematical program likh sakenge. Chaliye step-by-step samajhte hain ki yeh code kaise kaam karta hai!
🧠 Is Project mein kya seekhoge?
Yeh project banate waqt aap ye Python concepts use karoge:
| Concept | Kya karta hai |
|---|---|
input() | User se keyboard se data lena |
float() | Text ko decimal number mein convert karna |
if-elif-else | Conditions se decisions lena |
try-except | Errors ko handle karna |
f-string | Variables ko string mein include karna |
📝 Code kaise kaam karta hai — Step by Step
Neeche code ki poori logic step-by-step samjhayi gayi hai:
- User se 4 options mein se ek operation choose karvao
- Pehla aur doosra number input lo
- Chosen operation ke hisaab se calculation karo
- Result clearly print karo
- Agar galat input ho to error message do
⚠️ Common Mistakes — Bhool mat jaana!
Beginners yeh galtiyan aksar karte hain — dhyan rakho:
- ⚠️ int() ki jagah float() use karo — taki decimal numbers bhi kaam karein
- ⚠️ Division mein b==0 check karo — warna ZeroDivisionError aayega
- ⚠️ try-except se invalid input handle karo — warna program crash ho sakta hai
🏋️ Practice Exercises — Aage badho!
Yeh project complete karne ke baad in exercises se practice karo:
- 💡 5 operations add karo: modulus (%), power (**), square root
- 💡 Loop add karo taki user baar baar calculate kar sake
- 💡 History feature add karo — pichli calculations save ho
- 💡 Scientific calculator banao math module se
❓ Aksar Pooche Jane Wale Sawal (FAQ)
Q: Calculator mein decimal support kyon zaroori hai?
A: float() se 5.5 + 2.3 jaisi calculations bhi ho sakti hain. int() se sirf pura number hi daal sakte hain.
A: float() se 5.5 + 2.3 jaisi calculations bhi ho sakti hain. int() se sirf pura number hi daal sakte hain.
Q: ZeroDivisionError kya hota hai?
A: Jab koi bhi number ko 0 se divide karo to Python yeh error deta hai. Hamesha b != 0 check karo.
A: Jab koi bhi number ko 0 se divide karo to Python yeh error deta hai. Hamesha b != 0 check karo.
Q: try-except kab use karein?
A: Jab bhi user se input lo — user galat cheez bhi type kar sakta hai jaise letter number ki jagah.
A: Jab bhi user se input lo — user galat cheez bhi type kar sakta hai jaise letter number ki jagah.