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
C=5
def incrementbyC(num):
def updateC(a):
print "The incremented number is:15
incrementbyC(10)
print "The incremented number is:"110
updateC(100)
incrementbyC(10)
Answers
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 :
- Initialize a variable with 5, and write a function that takes a value and increments it by 5.
- The value needs 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 the global keyword, so that the constant value changes dynamically.
- And then, pass the same variable through the 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 analyzing 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