Computer Science, asked by nikilkumar00100, 11 months ago

Write a simple python function to increase the value of any number by a constant "C=5" and write another simple function to update the value of "C" to any other value like 6,7,8, etc.
# The required output is as:
The incremented number is:
15
The incremented number is:
110​

Answers

Answered by poojan
14

Program of python functions which updates a constant and increments a value.

Program :

c=5

def increasevalue(n):

   n=n+c

   return n

val=int(input("Enter a value to be incremented: "))

print("Before updation, the incremented value is : ",increasevalue(val))

def changeconstvalue(m):

   global c

   c=m

   return c

c=changeconstvalue(int(input("Enter the constant updation value: ")))

print("After updation, the incremented value is : ", increasevalue(val))

Input :

Enter a value to be incremented: 10

Enter the constant updation value: 100

 

Output :

Before updation, the incremented value is : 15

After updation, the incremented value is : 110

Explanation :

  • Intialize a variable with 5, and write a function that takes a value and increments it by 5.
  • The value need to be given by the user.
  • Next, take another user input about changing the constant value.
  • Then pass it through a function that has the capability of changing the constant using global keyword, so that the constant value changes dynamically.
  • And then, pass the same variable through incrementing function and this time, the value should be incremented by the updated increment value.

Note : I have added the attachments of the interpretation and the execution of programs with their outputs. Take a look!

Quick Tip : In python, you must and should follow Indentation. Following the indentation property in any programming language will help you in quickly analysing the code, if an error occurs.

Learn more :

  • Advantages of Programming in python

        brainly.in/question/11007952

  • Indentation is must in python. Know more about it at :

        brainly.in/question/17731168

   

Hope it helps. Cheers!

Attachments:
Answered by Anonymous
18

Here is a python program:

#First creating function to increase value constantly

C = 5

def increase_value(i):

  i = i+C

  return i

  Value = int(input("Enter the value to increment: "))

#Creating constant value updating function

def constant_val(v)

  global C

  C = v

  return C

C = constant_val(int(input("Enter the constant updating value: ")))

print("The incremented value is ", increase_value(Value), ".")

Testing the program:

Input:

Enter the value to increment:  15

Enter the constant updating value: 120

Output:

The incremented value is 135.

Explanation:

  • Initially, we declared a variable as 5. And then wrote a function to increment it's value by 5. (Input)
  • And we return the value by using return.
  • Nextly, we take another input from the user of which is to be constantly incremented. (Output)
  • Then we used the global keyword to change the constant value along with increment value.
  • Then we passed the incrementing value variable through the constant value function.
  • And we get the incremented value updated along with the constant value.

Some important tips:

  • If you will copy-paste the above program, then it will not work properly in IDE and editors.
  • You must indent the code properly before it is tested.
  • Also, note that While taking input from the user you must convert the input from str to int using int.
  • This program can be also written using without functions. But, it can be a lengthy code.
Similar questions