Recursive Assignment Countup and Countdown and also how to solve runtime error PDF

Title Recursive Assignment Countup and Countdown and also how to solve runtime error
Course Programming Fundamentals
Institution University of the People
Pages 4
File Size 76.1 KB
File Type PDF
Total Downloads 31
Total Views 123

Summary

How to work with recursive function count up that expects a negative argument and counts “down” from that number, how to solve a runtime error function in python The runtime error occurs when the interpreter tries to add an integer and a string while string has not been converted to an integer....


Description

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:

def countup(n): #implemented a function name parameter if (n==0): #count up untill to base 0 print("Blastoff!") # print blastoff if condition met else: print(n) # print value currently n is countup(n+1) # add recursive statement countup(-3) # call the function countup

OUTPUT: -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.)

num = input("please enter an integer:\n 5")#I have used 5 to assume the user has input that value num = int(num) print(num) OUTPUT: please enter an integer: 5 # I have used 5 to assume the user has input that value.

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. Countdown

def countdown(n):#implement a function name parameter if n0: # if number is greater than 0 count until 0 countdown(num) #countdown positive number else: print("Blastoff!") # print blastoff if condition met countdown(5) # call the function countdown OUTPUT: 5 4 3 2 1 Blastoff!

Countup def countup(n):#implement a function name parameter if n0: # if number is greater than 0 count until 0 countup(num) #countup positive number else: print("Blastoff!") # print blastoff if condition met countup(5) # call the function countup

OUTPUT: -5 -4 -3 -2 -1 Blastoff!

Explanation:

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.

n=30 v="20" def bottle(n): print(n + v) print(n,v) bottle(n) OUTPUT: Traceback (most recent call last): File "C:/Users/DJ & GFX/Desktop/run time error.py", line 8, in bottle(n) File "C:/Users/DJ & GFX/Desktop/run time error.py", line 5, in bottle print(n + v) TypeError: unsupported operand type(s) for +: 'int' and 'str' Explanation: we get a type error after the interpreter detects no syntax error. The runtime error occurs when the interpreter tries to add an integer and a string while string has not been converted to an integer. How to fix the error message: n=30 v="20" def bottle(n): print(n + int(v)) print(n,v) bottle(n) OUTPUT: 50 30 20 Explanation: you can fix the runtime error message by changing the string to an integer by add “int” in front of the string. This fixes the error....


Similar Free PDFs