*Error Handling and Debugging Part-3*
๐ *Fixing Broken Programs* โ
๐ This is very important for interviews and real jobs.
*What is โFix the Bugโ type problem*
You are given broken code.
You must:
- Find the error
- Understand why
- Fix it
Companies test problem-solving this way.
*Example 1. Division Bug*
โ Broken code
a = 10
b = 0
print(a / b)
Problem โ division by zero.
โ Fixed code
a = 10
b = 0
if b != 0:
print(a / b)
else:
print("Cannot divide by zero")
*Example 2. List Index Bug*
โ Broken code
numbers = [1, 2, 3]
print(numbers[3])
Problem โ index out of range.
โ Fixed code
numbers = [1, 2, 3]
if len(numbers) > 3:
print(numbers[3])
else:
print("Index not available")
Better fix:
print(numbers[-1])
*Example 3. Wrong Average Calculation (Logical Error)*
โ Broken code
marks = [70, 80, 90]
avg = sum(marks)
print(avg)
Problem โ forgot division.
โ Fixed code
avg = sum(marks) / len(marks)
print(avg)
*Example 4. Infinite Loop Bug*
โ Broken code
count = 1
while count <= 5:
print(count)
Problem โ variable never updated.
โ Fixed code
count = 1
while count <= 5:
print(count)
count += 1
*Example 5. Function Return Bug*
โ Broken code
def add(a, b):
print(a + b)
result = add(2, 3) + 5
Problem โ function returns None.
โ Fixed code
def add(a, b):
return a + b
result = add(2, 3) + 5
print(result)
*Real Interview Strategy*
โญ When fixing code:
1. Read error message carefully
2. Identify error type
3. Check variables and inputs
4. Test small parts
5. Fix root cause
Never guess.
*Common Bugs Interviewers Test*
- Off-by-one errors
- Missing return
- Wrong loop condition
- Index errors
- Division by zero
- Wrong variable usage
*Practice Tasks for You*
1. Fix program that crashes when list is empty
2. Fix function returning wrong value
3. Fix loop printing wrong numbers
4. Fix file open error if file missing
5. Fix string index error
*โ Practice Task Solutions*
*Task 1. Fix program that crashes when list is empty*
โ Problem โ accessing element from empty list.
โ Fixed code
numbers = []
if len(numbers) > 0:
print(numbers[0])
else:
print("List is empty")
*Task 2. Fix function returning wrong value*
โ Broken code
def square(n):
print(n * n)
Problem โ returns None.
โ Fixed code
def square(n):
return n * n
result = square(5)
print(result)
*Task 3. Fix loop printing wrong numbers*
โ Broken code
for i in range(1, 5):
print(i)
Problem โ does not include 5.
โ Fixed code
for i in range(1, 6):
print(i)
*Task 4. Fix file open error if file missing*
โ Problem โ program crashes if file not found.
โ Fixed code
try:
with open("data.txt", "r") as file:
print(file.read())
except FileNotFoundError:
print("File not found")
*Task 5. Fix string index error*
โ Broken code
text = "Python"
print(text[10])
Problem โ index out of range.
โ Fixed code
text = "Python"
index = 10
if index < len(text):
print(text[index])
else:
print("Index out of range")
*One-line memory rule*
Debugging = Observe โ Understand โ Fix.
*Double Tap โฅ๏ธ For More*
๐ *Fixing Broken Programs* โ
๐ This is very important for interviews and real jobs.
*What is โFix the Bugโ type problem*
You are given broken code.
You must:
- Find the error
- Understand why
- Fix it
Companies test problem-solving this way.
*Example 1. Division Bug*
โ Broken code
a = 10
b = 0
print(a / b)
Problem โ division by zero.
โ Fixed code
a = 10
b = 0
if b != 0:
print(a / b)
else:
print("Cannot divide by zero")
*Example 2. List Index Bug*
โ Broken code
numbers = [1, 2, 3]
print(numbers[3])
Problem โ index out of range.
โ Fixed code
numbers = [1, 2, 3]
if len(numbers) > 3:
print(numbers[3])
else:
print("Index not available")
Better fix:
print(numbers[-1])
*Example 3. Wrong Average Calculation (Logical Error)*
โ Broken code
marks = [70, 80, 90]
avg = sum(marks)
print(avg)
Problem โ forgot division.
โ Fixed code
avg = sum(marks) / len(marks)
print(avg)
*Example 4. Infinite Loop Bug*
โ Broken code
count = 1
while count <= 5:
print(count)
Problem โ variable never updated.
โ Fixed code
count = 1
while count <= 5:
print(count)
count += 1
*Example 5. Function Return Bug*
โ Broken code
def add(a, b):
print(a + b)
result = add(2, 3) + 5
Problem โ function returns None.
โ Fixed code
def add(a, b):
return a + b
result = add(2, 3) + 5
print(result)
*Real Interview Strategy*
โญ When fixing code:
1. Read error message carefully
2. Identify error type
3. Check variables and inputs
4. Test small parts
5. Fix root cause
Never guess.
*Common Bugs Interviewers Test*
- Off-by-one errors
- Missing return
- Wrong loop condition
- Index errors
- Division by zero
- Wrong variable usage
*Practice Tasks for You*
1. Fix program that crashes when list is empty
2. Fix function returning wrong value
3. Fix loop printing wrong numbers
4. Fix file open error if file missing
5. Fix string index error
*โ Practice Task Solutions*
*Task 1. Fix program that crashes when list is empty*
โ Problem โ accessing element from empty list.
โ Fixed code
numbers = []
if len(numbers) > 0:
print(numbers[0])
else:
print("List is empty")
*Task 2. Fix function returning wrong value*
โ Broken code
def square(n):
print(n * n)
Problem โ returns None.
โ Fixed code
def square(n):
return n * n
result = square(5)
print(result)
*Task 3. Fix loop printing wrong numbers*
โ Broken code
for i in range(1, 5):
print(i)
Problem โ does not include 5.
โ Fixed code
for i in range(1, 6):
print(i)
*Task 4. Fix file open error if file missing*
โ Problem โ program crashes if file not found.
โ Fixed code
try:
with open("data.txt", "r") as file:
print(file.read())
except FileNotFoundError:
print("File not found")
*Task 5. Fix string index error*
โ Broken code
text = "Python"
print(text[10])
Problem โ index out of range.
โ Fixed code
text = "Python"
index = 10
if index < len(text):
print(text[index])
else:
print("Index out of range")
*One-line memory rule*
Debugging = Observe โ Understand โ Fix.
*Double Tap โฅ๏ธ For More*
โค1
๐
๐ด๐ฝ๐พ๐ผ ๐
๐ด๐ฒ๐ท ๐พ๐ต๐ต๐ธ๐ฒ๐ธ๐ฐ๐ป
NEW BOT JUST CLICK START โ @Billydash_bot
Don't forget to check โ๏ธ out
Add more members for giveaways โบ๏ธโบ๏ธ
This media is not supported in your browser
VIEW IN TELEGRAM
First 2 comments
Come @papparazic1
Airtel unlimited test coming up...๐ฅ๐ฅ
Stay tune๐๐ฐ๐ชโ๏ธ
Stay tune๐๐ฐ๐ชโ๏ธ
๐ฅฐ2
๐๐๐ฃ๐๐จ๐ง ๐ณ ๐๐๐ฌ๐ฆ ๐๐ฅ๐๐ ๐ง๐ฐ๐
๐๐ฐ๐ก - 542418xxxxxxxxxx
๐๐ฃ - ๐จ๐ฆ๐
๐๐ฐ๐ก - 542418xxxxxxxxxx
๐๐ฃ - ๐จ๐ฆ๐
This media is not supported in your browser
VIEW IN TELEGRAM
First person to send mail at 10:00 EAT for the Spotify giveaway
Expressvpn Accs by @itachixsr.txt
4.4 KB
31X EXPRESSVPN PREMIUM ONLY MOBILE APP
Anyone can create fresh mail dm @papparazic1
๐ง๐ท AD0BE FREEEE T4L WORK4NG
โ 441105013819xxxx|02|31
โ 44110501381xxxxx|02|31
/gen in gc
โ IP : NO VPN
โ ZIP : WC2E 9TB
๐ค Country : United Kingdom
๐ค OFFER LINK : CLICK HERE
โ 441105013819xxxx|02|31
โ 44110501381xxxxx|02|31
/gen in gc
โ IP : NO VPN
โ ZIP : WC2E 9TB
๐ค Country : United Kingdom
๐ค OFFER LINK : CLICK HERE
PARAMOUNT PLUS
5579070155xxxxxx
Bn:
FECA: 05|2029*
*CV: RND*
IP:Mรฉxico๐ฒ๐ฝ
Calle 57 No. 464-A
Barrio de La Mejorada
Ciudad: Mรฉrida
Estado: Yucatรกn
Cรณdigo Postal: 97000
LINK: https://www.paramountplus.com/mx/
5579070155xxxxxx
Bn:
FECA: 05|2029*
*CV: RND*
IP:Mรฉxico๐ฒ๐ฝ
Calle 57 No. 464-A
Barrio de La Mejorada
Ciudad: Mรฉrida
Estado: Yucatรกn
Cรณdigo Postal: 97000
LINK: https://www.paramountplus.com/mx/
Paramount+
Paramount+ Mรฉxico | Ver pelรญculas y series en lรญnea
Ve pelรญculas exitosas, nuevos originales y tus series favoritas con Paramount+. Una montaรฑa de entretenimiento. ยกEmpieza a ver cualquier dispositivo!
๐ต HotSpot Shield
f4bf634u10@gmail.com
f4bf634u16@gmail.com
f4bf634u58@gmail.com
Password: 1@Ghachvpn
โ ๏ธ Please do not change the password.
๐ Plan: Premium
๐ Expiry : 7 Days
๐ต Device : 10
f4bf634u10@gmail.com
f4bf634u16@gmail.com
f4bf634u58@gmail.com
Password: 1@Ghachvpn
โ ๏ธ Please do not change the password.
๐ Plan: Premium
๐ Expiry : 7 Days
๐ต Device : 10