Computer Science
Write a program with a function that takes an integer and prints the number that follows after it. Call the function with these arguments :
4, 6, 8, 2 + 1, 4 - 3 * 2, -3 -2
Python Functions
6 Likes
Answer
1. def print_number(number):
2. next_number = number + 1
3. print("The number following", number, "is", next_number)
4. print_number(4)
5. print_number(6)
6. print_number(8)
7. print_number(2 + 1)
8. print_number(4 - 3 * 2)
9. print_number(- 3 - 2)
Output
The print_number following 4 is 5
The print_number following 6 is 7
The print_number following 8 is 9
The print_number following 3 is 4
The print_number following -2 is -1
The print_number following -5 is -4
Explanation
def print_number(number)
— This line defines a function namedprint_number
that takes one argumentnumber
.next_number = number + 1
— Inside theprint_number
function, this line calculates the next number after the input number by adding 1 to it and assigns the result to the variablenext_number
.print("The number following", number, "is", next_number)
— Inside theprint_number
function, this line prints a message stating the number and its following number.Then the
print_number
function is called multiple times with 4, 6, 8, 3 ,((4 - 3 * 2) = -2), ((-3-2) = -5) as arguments.
Answered By
1 Like
Related Questions
Find and write the output of the following python code :
a = 10 def call(): global a a = 15 b = 20 print(a) call()
In the following code, which variables are in the same scope ?
def func1(): a = 1 b = 2 def func2(): c = 3 d = 4 e = 5
Write a program with non-void version of above function and then write flow of execution for both the programs.
What is the output of following code fragments ?
def increment(n): n.append([4]) return n L = [1, 2, 3] M = increment(L) print(L, M)