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

lOMoARcPSD|5493832

CS 1101 - AY2020-T5 Learning Journal Unit 3

Programming Fundamentals (University of the People)

StuDocu is not sponsored or endorsed by any college or university


Downloaded by sidharth h (sidharthh009@gmail.com)
lOMoARcPSD|5493832

Learning Journal Unit 3


1. Copy the countdown function from Section 5.8 of your textbook.

def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)

Write a new recursive function countup that expects a negative argument and counts “up” from that
number. Output from running the function should look something like this:

>>> countup(-3)
-3
-2
-1
Blastoff!

Write a Python program that gets a number using keyboard input. (Remember to use input for Python
3 but raw_input for Python 2.)

If the number is positive, the program should call countdown. If the number is negative, the program
should call countup. Choose for yourself which function to call (countdown or countup) for input of
zero.

Provide the following.

The code of your program.


Output for the following input: a positive number, a negative number, and zero.
An explanation of your choice for what to call for input of zero.

/
Downloaded by sidharth h (sidharthh009@gmail.com)
lOMoARcPSD|5493832

2. Write your own unique Python program that has a runtime error. Do not copy the program from
your textbook or the Internet. Provide the following.

The code of your program.


Output demonstrating the runtime error, including the error message.
An explanation of the error message.
An explanation of how to fix the error.

Submission status

Attempt number This is attempt 1.

Submission status Submitted for grading

Grading status Graded

Due date Friday, 10 July 2020, 12:55 PM

Time remaining Assignment was submitted 3 days 17 hours early

Last modified Monday, 6 July 2020, 7:30 PM

Online text

Question 1(i):

Copy the countdown function from Section 5.8 of your textbook.

def countdown(n):
   if n <= 0:
        print('Blastoff!')
   else:
        print(n)
        countdown(n-1)

(i) Write a new recursive function countup that expects a negative argument


and counts “up” from that number. Output from running the function should
look something like this:

>>> countup(-3)
-3
-2
-1
Blastoff!

Answer 1(i):

Input
# countup recursive function
def countup(n):
if n >= 0:
print('Blastoff!')
else:
print(n)
countup(n + 1)


/
Downloaded by sidharth h (sidharthh009@gmail.com)
lOMoARcPSD|5493832

Output at console
countup(-3)
-3
-2
-1
Blastoff!

Question 1(ii):

Write a Python program that gets a number using keyboard input. (Remember


to use input for Python 3 but raw_input for Python 2.)

If the number is positive, the program should call countdown. If the number is


negative, the program should call countup. Choose for yourself which function
to call (countdown or countup) for input of zero.

Provide the following.

·         The code of your program.

·         Output for the following input: a positive number, a negative number,
and zero.

·         An explanation of your choice for what to call for input of zero.

Answer 1 (ii) :


/
Downloaded by sidharth h (sidharthh009@gmail.com)
lOMoARcPSD|5493832

# Input/Code of program
# countdown recursive function
def countdown(n): #countdown function is defined
if n <= 0:
print('Blastoff!') #Blassoff! phrase printed at output when n
equal or less than zero
else:
print(n) #value of n is printed at output when n is greate
r than zero
countdown(n - 1) #value of n-1 is computed to make it close
r to its base case, n = 0

# countup recursive function


def countup(n): #countup function is defined
if n >= 0:
print('Blastoff!') #Blassoff! phrase printed at output when n
equal or greater than zero
else:
print(n) #value of n is printed at output when n is less than
zero
countup(n + 1) #value of n+1 is computed to make it closer to
its base case, n = 0

# (ii) keyboard input

number = int(input("What number do you wish to start off with?\n"))


if number <= 0:
countup(number) #for input negative value and zero value, countup
() function is called
else :
countdown(number) #for input positive value countdown() function
is called

Output (a):
(a) Positive number 2, countdown() function is called
What number do you wish to start off with countdown?
>?2
2
1
Blastoff!

Output (b):
(b) Negative number -2, countup() function is called
What number do you wish to start off with countdown?
>?-2
-2
-1
Blastoff!

/
Downloaded by sidharth h (sidharthh009@gmail.com)
lOMoARcPSD|5493832

Output (c):
(c) Zero 0, countup() function is called as zero meet the condition o
f the first branch which call countup() function
What number do you wish to start off with countdown?
>?0
Blastoff!

An explanation of my choice for what to call for input of zero. :


For input of zero, I have called countup() function as zero input meet the
condition of the first branch of conditional statement which call countup()
function.

Question 2:

Write your own unique Python program that has a runtime error. Do not copy
the program from your textbook or the Internet. Provide the following.

·         The code of your program.

·         Output demonstrating the runtime error, including the error message.

·         An explanation of the error message.

·         An explanation of how to fix the error.

Answer 2:
Code of Program:

#Code of program
def p_cBarDegSimple(P1,T1):
if P >= 10 and T1 >= 100:
print('This is high pressure and high temperature process. Ri
sk level 4.')
elif P1 >= 10 and T1 < 100:
print('This is high pressure but low temperature process. Ris
k level 3.')
elif P1 < 10 and T1 >= 100:
print('This is low pressure and high temperature process. Ris
k level 2.')
elif P1 < 10 and T1 < 100:
print('This is low pressure and low temperature process. Risk
level 1.')

p_cBarDegSimple(1,2)

Output:


/
Downloaded by sidharth h (sidharthh009@gmail.com)
lOMoARcPSD|5493832

Traceback (most recent call last):


File "C:/Users/Hp/Desktop/University of The People/2019-2020 Term
5/CS-1101 PROGRAMMING FUNDAMENTALS/Week 3 Unit 3 - Conditionals and R
ecursion/Discussion Assignment Week 3/Runtime_error.py", line 11, in
p_cBarDegSimple(1,2)
File "C:/Users/Hp/Desktop/University of The People/2019-2020 Term
5/CS-1101 PROGRAMMING FUNDAMENTALS/Week 3 Unit 3 - Conditionals and R
ecursion/Discussion Assignment Week 3/Runtime_error.py", line 2, in p
_cBarDegSimple
if P >= 10 and T1 >= 100:
NameError: name 'P' is not defined

Explanation on the error:


The runtime error occurs due to using an identifier which has not been
defined. Variable P is not defined in the p_cBarDegSimple() function. The
parameters used for p_cBarDegSimple() are P1 and T1 to pass the arguments
to the function. Since variable P is not defined in the function, the code
execute the branch containing variable P cannot properly.

Explanation of how to fix the error:

To fix the runtime error, we need to update the variable name of P to P1 or


change all the P1 variables to P instead. Either way will work. I would choose
to update the variable name of P to P1 on the first branch of if statement to
ease my work. Corrected program and its output are shown below:

#Corrected Code of program


def p_cBarDegSimple(P1,T1):
if P1 >= 10 and T1 >= 100:
print('This is high pressure and high temperature process. Ri
sk level 4.')
elif P1 >= 10 and T1 < 100:
print('This is high pressure but low temperature process. Ris
k level 3.')
elif P1 < 10 and T1 >= 100:
print('This is low pressure and high temperature process. Ris
k level 2.')
elif P1 < 10 and T1 < 100:
print('This is low pressure and low temperature process. Risk
level 1.')

p_cBarDegSimple(1,2)

Output of corrected program:

This is low pressure and low temperature process. Risk level 1.

Submission
Comments (0)
comments


/
Downloaded by sidharth h (sidharthh009@gmail.com)

You might also like