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
  1. def print_number(number) — This line defines a function named print_number that takes one argument number.

  2. next_number = number + 1 — Inside the print_number function, this line calculates the next number after the input number by adding 1 to it and assigns the result to the variable next_number.

  3. print("The number following", number, "is", next_number) — Inside the print_number function, this line prints a message stating the number and its following number.

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