Nothing Special   »   [go: up one dir, main page]

Assignment 4 PIP

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Department of Computer Science & Engineering

Faculty of Engineering & Technology


1. Consider the following Python code intended to compute the sum of n natural numbers. During testing, it
was found that sum printed by program always excludes the last number. Debug the following script using
the debugger.
Program to compute the sum of n natural numbers.

Step by Step Debugging:


Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
========== RESTART: /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py =========
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(4)<module>()
-> def summation(n):
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(10)<module>()
-> def main():
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(15)<module>()
-> if __name__ == '__main__':
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(16)<module>()
-> main()
(Pdb) s
--Call--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(10)main()
-> def main():
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(11)main()
-> n = int(input("Enter number of terms: "))
(Pdb) n
Enter number of terms: 5
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(12)main()
-> total = summation(n)
(Pdb) s
--Call--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(4)summation()
-> def summation(n):
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(5)summation()
-> total = 0
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(6)summation()
-> for count in range(1, n):
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(7)summation()
-> total += count
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(6)summation()
-> for count in range(1, n):
(Pdb) p total
1
(Pdb) s

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166


Department of Computer Science & Engineering
Faculty of Engineering & Technology
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(7)summation()
-> total += count
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(6)summation()
-> for count in range(1, n):
(Pdb) p total
3
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(7)summation()
-> total += count
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(6)summation()
-> for count in range(1, n):
(Pdb) p total
6
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(7)summation()
-> total += count
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(6)summation()
-> for count in range(1, n):
(Pdb) p total
10
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(8)summation()
-> return total
(Pdb) s
--Return--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py(8)summation()->10
-> return total
(Pdb) q
Traceback (most recent call last):
File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
exec(code, self.locals)
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py", line 16, in <module>
main()
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py", line 12, in main
total = summation(n)
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q1.py", line 8, in summation
return total
File "/usr/lib/python3.10/bdb.py", line 94, in trace_dispatch
return self.dispatch_return(frame, arg)
File "/usr/lib/python3.10/bdb.py", line 156, in dispatch_return
if self.quitting: raise BdbQuit
bdb.BdbQuit

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166


Department of Computer Science & Engineering
Faculty of Engineering & Technology

Previous Code Corrected Code


import pdb import pdb
pdb.set_trace() pdb.set_trace()

def summation(n): def summation(n):


total = 0 total = 0
for count in range(1, n): for count in range(1, n + 1):
total += count total += count
return total return total

def main(): def main():


n = int(input("Enter number of terms: n = int(input("Enter number of terms:
")) "))
total = summation(n) total = summation(n)
print("Sum of first",n,"positive print("Sum of first",n,"positive
integers: ",total) integers: ",total)

if __name__ == '__main__': if __name__ == '__main__':


main() main()

2. Consider the following Python code intended to print inverse right triangle for given numbers of rows
nRows. For example, for nRows = 5, the following inverted triangle should be printed:
*****
****
***
**
*
During testing, it was found that the program does not produce even the single line of output. Debug
the following script using the debugger.
Program to print inverse right triangle.
Step by Step Debugging:
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
========== RESTART: /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py =========
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py(3)<module>()
-> def invertedRightTriangle(nRows):
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py(7)<module>()
-> def main():
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py(11)<module>()
-> if __name__ == '__main__':
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py(12)<module>()
-> main()
(Pdb) s

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166


Department of Computer Science & Engineering
Faculty of Engineering & Technology

--Call--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py(7)main()
-> def main():
(Pdb) n
Enter number of rows: 5
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py(9)main()
-> invertedRightTriangle(nRows)
(Pdb) s
--Call--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py(3)invertedRightTriangle()
-> def invertedRightTriangle(nRows):
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py(4)invertedRightTriangle()
-> for i in range(nRows, 0):
(Pdb) s
--Return--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py(4)invertedRightTriangle()->None
-> for i in range(nRows, 0):
(Pdb) q
Traceback (most recent call last):
File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
exec(code, self.locals)
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py", line 12, in <module>
main()
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py", line 9, in main
invertedRightTriangle(nRows)
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q2.py", line 4, in
invertedRightTriangle
for i in range(nRows, 0):
File "/usr/lib/python3.10/bdb.py", line 94, in trace_dispatch
return self.dispatch_return(frame, arg)
File "/usr/lib/python3.10/bdb.py", line 156, in dispatch_return
if self.quitting: raise BdbQuit
bdb.BdbQuit

Previous Code Corrected Code


import pdb import pdb
pdb.set_trace() pdb.set_trace()
def invertedRightTriangle(nRows): def invertedRightTriangle(nRows):
for i in range(nRows, 0): for i in range(nRows, 0, -1):
print("*"*i); print("*"*i);

def main(): def main():


nRows = int(input("Enter number of nRows = int(input("Enter number of
rows: ")) rows: "))
invertedRightTriangle(nRows) invertedRightTriangle(nRows)

if __name__ == '__main__': if __name__ == '__main__':


main() main()

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166


Department of Computer Science & Engineering
Faculty of Engineering & Technology

3. Consider the Python script given below intended to compute the percentage. During testing, it was
found that percentage computed was not accurate rather rounded to lower bound integer value. Debug
the following script using the debugger.
Program to print inverse right triangle
Step by Step Debugging:
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
========== RESTART: /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py =========
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(3)<module>()
-> def main():
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(20)<module>()
-> if __name__ == '__main__':
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(21)<module>()
-> main()
(Pdb) s
--Call--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(3)main()
-> def main():
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(4)main()
-> totalMarks = 0
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(5)main()
-> i = 0
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(6)main()
-> while True:
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(7)main()
-> marks = input("Enter marks for subject "+str(i+1)+":")
(Pdb) n
Enter marks for subject 1:75
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(8)main()
-> if marks=='':
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(10)main()
-> marks = int(marks)
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(11)main()
-> if marks<0 or marks>100:
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(14)main()
-> i += 1
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(15)main()
-> totalMarks += marks
(Pdb) s

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166


Department of Computer Science & Engineering
Faculty of Engineering & Technology

> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(7)main()


-> marks = input("Enter marks for subject "+str(i+1)+":")
(Pdb) n
Enter marks for subject 2:80
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(8)main()
-> if marks=='':
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(10)main()
-> marks = int(marks)
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(11)main()
-> if marks<0 or marks>100:
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(14)main()
-> i += 1
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(15)main()
-> totalMarks += marks
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(6)main()
-> while True:
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(7)main()
-> marks = input("Enter marks for subject "+str(i+1)+":")
(Pdb) n
Enter marks for subject 3:
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(8)main()
-> if marks=='':
(Pdb)
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(9)main()
-> break
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(16)main()
-> percentage = totalMarks // i
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(17)main()
-> print("Total Marks: ",int(totalMarks))
(Pdb) s
--Call--
> /usr/lib/python3.10/idlelib/run.py(462)write()
-> def write(self, s):
(Pdb) u
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(17)main()
-> print("Total Marks: ",int(totalMarks))
(Pdb) n
Total Marks: 155
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(18)main()
-> print("Percentage: ",round(percentage, 2))
(Pdb) n
Percentage: 77
--Return--

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166


Department of Computer Science & Engineering
Faculty of Engineering & Technology

> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py(18)main()->None


-> print("Percentage: ",round(percentage, 2))
(Pdb) q
Traceback (most recent call last):
File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
exec(code, self.locals)
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py", line 21, in <module>
main()
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q3.py", line 18, in main
print("Percentage: ",round(percentage, 2))
File "/usr/lib/python3.10/bdb.py", line 94, in trace_dispatch
return self.dispatch_return(frame, arg)
File "/usr/lib/python3.10/bdb.py", line 156, in dispatch_return
if self.quitting: raise BdbQuit
bdb.BdbQuit

Previous Code Corrected Code


import pdb import pdb
pdb.set_trace() pdb.set_trace()
def main(): def main():
totalMarks = 0 totalMarks = 0
i = 0 i = 0
while True: while True:
marks = input("Enter marks for marks = input("Enter marks for
subject "+str(i+1)+":") subject "+str(i+1)+":")
if marks=='': if marks=='':
break break
marks = int(marks) marks = int(marks)
if marks<0 or marks>100: if marks<0 or marks>100:
print("Invalid Marks") print("Invalid Marks")
continue continue
i += 1 i += 1
totalMarks += marks totalMarks += marks
percentage = totalMarks // i percentage = totalMarks / i
print("Total Marks: print("Total Marks:
",int(totalMarks)) ",int(totalMarks))
print("Percentage: print("Percentage:
",round(percentage, 2)) ",round(percentage, 2))

if __name__ == '__main__': if __name__ == '__main__':


main() main()

4. Consider the Python given below intended to determine whether the given year is a leap year. During
testing, it was found that an year such as 1800 or 2100, despite being non-leap year, was also displayed
as a leap-year. Debug the following script using the debugger.
Program to print Leap Year

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166


Department of Computer Science & Engineering
Faculty of Engineering & Technology

Step by Step Debugging:


Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
========== RESTART: /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py =========
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(4)<module>()
-> def isLeapYear(year):
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(7)<module>()
-> def main():
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(11)<module>()
-> if __name__ == '__main__':
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(12)<module>()
-> main()
(Pdb) s
--Call--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(7)main()
-> def main():
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(8)main()
-> year = int(input("Enter Year "))
(Pdb) n
Enter Year 2100
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(9)main()
-> print("It is a leap year: ", isLeapYear(year))
(Pdb) n
It is a leap year: True
--Return--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(9)main()->None
-> print("It is a leap year: ", isLeapYear(year))
(Pdb) q
Traceback (most recent call last):
File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
exec(code, self.locals)
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py", line 12, in <module>
main()
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py", line 9, in main
print("It is a leap year: ", isLeapYear(year))
File "/usr/lib/python3.10/bdb.py", line 94, in trace_dispatch
return self.dispatch_return(frame, arg)
File "/usr/lib/python3.10/bdb.py", line 156, in dispatch_return
if self.quitting: raise BdbQuit
bdb.BdbQuit

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166


Department of Computer Science & Engineering
Faculty of Engineering & Technology

Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux


Type "help", "copyright", "credits" or "license()" for more information.
========== RESTART: /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py =========
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(4)<module>()
-> def isLeapYear(year):
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(7)<module>()
-> def main():
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(11)<module>()
-> if __name__ == '__main__':
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(12)<module>()
-> main()
(Pdb) s
--Call--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(7)main()
-> def main():
(Pdb) s
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(8)main()
-> year = int(input("Enter Year "))
(Pdb) n
Enter Year 2100
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(9)main()
-> print("It is a leap year: ", isLeapYear(year))
(Pdb) n
It is a leap year: True
--Return--
> /media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py(9)main()->None
-> print("It is a leap year: ", isLeapYear(year))
(Pdb) q
Traceback (most recent call last):
File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
exec(code, self.locals)
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py", line 12, in <module>
main()
File "/media/vivek/Vivek/3rd Year/PIP/Assignment 4/Q4.py", line 9, in main
print("It is a leap year: ", isLeapYear(year))
File "/usr/lib/python3.10/bdb.py", line 94, in trace_dispatch
return self.dispatch_return(frame, arg)
File "/usr/lib/python3.10/bdb.py", line 156, in dispatch_return
if self.quitting: raise BdbQuit
bdb.BdbQuit

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166


Department of Computer Science & Engineering
Faculty of Engineering & Technology

Previous Code Corrected Code


import pdb import pdb
pdb.set_trace() pdb.set_trace()

def isLeapYear(year): def isLeapYear(year):


return year%400==0 or year%100==0 or return year%400==0 and year%100==0 or
year%4==0 year%4==0

def main(): def main():


year = int(input("Enter Year ")) year = int(input("Enter Year "))
print("It is a leap year: ", print("It is a leap year: ",
isLeapYear(year)) isLeapYear(year))

if __name__ == '__main__': if __name__ == '__main__':


main() main()

5. Consider the Python script given below intended to find HCF. During testing, it was found that program
yields an error for numbers having no common factor other than 1. Debug the following script
using the debugger.
Program to find HCF of two numbers

Step by Step Debugging:


Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
=================== RESTART: /home/vivek/Documents/PIP/Q4.py ===================
> /home/vivek/Documents/PIP/Q4.py(4)<module>()
-> def findHCF(num1, num2):
(Pdb) s
> /home/vivek/Documents/PIP/Q4.py(14)<module>()
-> def main():
(Pdb) s
> /home/vivek/Documents/PIP/Q4.py(19)<module>()
-> if __name__ == '__main__':
(Pdb) s
> /home/vivek/Documents/PIP/Q4.py(20)<module>()
-> main()
(Pdb) s
--Call--
> /home/vivek/Documents/PIP/Q4.py(14)main()
-> def main():
(Pdb) s
> /home/vivek/Documents/PIP/Q4.py(15)main()
-> num1 = int(input("Enter first number: "))
(Pdb) n
Enter first number: 5

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166


Department of Computer Science & Engineering
Faculty of Engineering & Technology

> /home/vivek/Documents/PIP/Q4.py(16)main()
-> num2 = int(input("Enter second number: "))
(Pdb) n
Enter second number: 13
> /home/vivek/Documents/PIP/Q4.py(17)main()
-> print(findHCF(num1,num2))
(Pdb) n
UnboundLocalError: local variable 'HCF' referenced before assignment
> /home/vivek/Documents/PIP/Q4.py(17)main()
-> print(findHCF(num1,num2))
(Pdb) q
Traceback (most recent call last):
File "/usr/lib/python3.10/idlelib/run.py", line 578, in runcode
exec(code, self.locals)
File "/home/vivek/Documents/PIP/Q4.py", line 20, in <module>
main()
File "/home/vivek/Documents/PIP/Q4.py", line 17, in main
print(findHCF(num1,num2))
File "/usr/lib/python3.10/bdb.py", line 96, in trace_dispatch
return self.dispatch_exception(frame, arg)
File "/usr/lib/python3.10/bdb.py", line 176, in dispatch_exception
if self.quitting: raise BdbQuit
bdb.BdbQuit

Previous Code Corrected Code


import pdb import pdb
pdb.set_trace() pdb.set_trace()

def findHCF(num1, num2): def findHCF(num1, num2):


if num1<num2: if num1<num2:
minNum = num1 minNum = num1
else: else:
minNum = num2 minNum = num2
for i in range (minNum, 1, -1): for i in range (1, minNum + 1):
if num1%i == 0 and num2%i == 0: if num1%i == 0 and num2%i == 0:
HCF = i HCF = i
return HCF return HCF

def main(): def main():


num1 = int(input("Enter first number: num1 = int(input("Enter first number:
")) "))
num2 = int(input("Enter second num2 = int(input("Enter second
number: ")) number: "))
print(findHCF(num1,num2)) print(findHCF(num1,num2))

if __name__ == '__main__': if __name__ == '__main__':


main() main()

Name: Vivek Kumar Mohanta Regd. Number. : 2141013166

You might also like