In: Computer Science
PYTHON - For loop - program must have proper docstring.
1/1 + 1/2 + 1/4 +1/4 + 1/8 + 1/8 + 1/8 + . . . + 1/n
The program should ask the user to input a number for n.
Program should perform range check on the input, for it to be from 1 to 100,000,000. I
f user input in not in the range, prompt the user until correct number is given.
The program should call the function with the user supplied value, then print the the returned value.
The python program for the given problem is given below with proper check function .
If the user input is not in range the again call the main function for proper input,
The code is given below.
CODE.
##defining the function docstring
###in the function we are taking the range upto value 1 to n
###and add in form 1/1+1/2+1/3+1/4+....+1/n.
def docstring_calc(n):
sum1=0
for i in range(1,n+1):
sum1+=(1/i)
return sum1 ##returning the result value
def main(): ##main function
###taking user input to it
n=int(input("Enter the value of n: "))
##check for input in range.
##if input are not in range then again call the main function
if n<1 or n>100000000:
print("Enter valid input.")
print()
main()
else: ##else call the function docstring
### and store the value in result
result=docstring_calc(n)
print("The Result is",result) ##print the result
### driver function###
### calling main function
main()
SNAPS.
For any query message me in a comment.