Professional Documents
Culture Documents
Cs 1101 Ay2020 t5 Learning Journal Unit 3
Cs 1101 Ay2020 t5 Learning Journal Unit 3
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.
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.
Submission status
Online text
Question 1(i):
def countdown(n):
if n <= 0:
print('Blastoff!')
else:
print(n)
countdown(n-1)
>>> 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):
· 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
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!
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.
· Output demonstrating the runtime error, including the error message.
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
p_cBarDegSimple(1,2)
Submission
Comments (0)
comments
/
Downloaded by sidharth h (sidharthh009@gmail.com)