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

Programming fundamental assignment

Q1: 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!

Solution

#count up function

>>>def count_up(x):

if x>=0:

print("Blastoff!")

else:

print (x)

count_up(x+1)

>>>count_up(-3)
Code output

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.

 Respective output for the following inputs: a positive number, a negative number,

and zero.

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


Solution

>>>def count_down(x):

if x<=0:

print("Blastoff!")

else:

print (x)

count_down(x-1)

>>>#count up function

>>>def count_up(x):

if x>=0:

print("Blastoff!")

else:

print (x)

count_up(x+1)

>>>x= int(input("Please enter your number "))

if x<0:

count_up(x)

elif x>0:

count_down(x)

elif x==0:

count_up(x)

else:
print("your input is invalid, please try again!")

This code allows the user to input a number of their choice. If the number is a negative, that is
less than zero, the count up function is executed, and if the number is a positive one that is
greater than zero, the countdown code is executed. In my code I have included another condition
that calls the count up function when the number entered is zero. In this case, python will just
output Blastoff! Since there is no count down or count up from zero. The example of this
scenario is shown below.

Code
Output of the code

Q2: You are developing a program that performs a division operation on two numbers
provided by the user. However, there is a situation where a runtime error can occur due to
a division by zero. To help junior developers learn about error handling in expressions and
conditions, you want to create a program deliberately containing this error and guide them
in diagnosing and fixing it.

Solution

>>>#Division by zero

>>>try:

x = int(input("please enter your first number "))

y = int(input("Please enter your second number "))

results = x/y

print(x ,"/" ,y ,"=", results)

>>>except ZeroDivisionError:

print("You cannot divide a number by zero, try a different value!")


To handle the division by zero error, we use the try …except block in python. Python will
execute the code but when there is division by zero, it outputs the error message (Severence,
2016)

In this case, when the second number is zero, the error message of “You cannot divide a number
by zero, try a different value!” will be displayed as shown.

If the try…except block could not have been used as shown below,

This could result to a runtime error as shown below.


Error handling is essential in programming as it helps one to take care of the unexpected

situation and prevent your code from crushing. When there is no error handling, your code will

display a traceback error message which may be confusing to junior developers since they may

not know exactly how to correct the error.

Through implantation of error handling, a clear error message is displayed to tell the junior

developers what happened and how they can correct their mistakes. It helps in taking alternatives

and this maintains a stable code that will not crash when it is run.

References

Downey, A. (2015). Think Python: How to think like a computer scientist. Green Tree Press

Severance, C. (2016). Python for Everybody Exploring Data Using Python 3.

https://dlib.hust.edu.vn/handle/HUST/21815

You might also like