Question

In: Computer Science

PYTHON - For loop - program must have proper docstring. 1/1 + 1/2 + 1/4 +1/4...

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.

Solutions

Expert Solution

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.


Related Solutions

2) create a python program that uses a for loop and range to print out the...
2) create a python program that uses a for loop and range to print out the values 10 8 6 4 2 3) Create a python program that yses a for loop to print out ["bob","al","bert"]
Python Create Loop 1 = 1 1 + 2 = 3 1 + 2 + 3...
Python Create Loop 1 = 1 1 + 2 = 3 1 + 2 + 3 = 6 1 + 2 + 3 + 4 = 10 1 + 2 + 3 + 4 + 5 = 15 Create a loop in python that makes this pattern
I have a python program that must check if an integer is the sum of the...
I have a python program that must check if an integer is the sum of the squares of four consecutive prime numbers. However, when I run the program, it goes into an infinite while loop, and it doesn't give me any answer. I can only use the while, if, else, True and False commands. The code I'm using is n1=2   n2=3   n3=5   n4=7   n = int(input("n: "))   if (n==(n1**2)+(n2**2)+(n3**2)+(n4**2)):     print(n1,n2,n3,n4)   else :     i = 2     pr = True     next =...
Program must use Python 3 Your program must have a welcome message for the user. Your...
Program must use Python 3 Your program must have a welcome message for the user. Your program must have one class called CashRegister. Your program will have an instance method called addItem which takes one parameter for price. The method should also keep track of the number of items in your cart. Your program should have two getter methods. getTotal – returns totalPrice getCount – returns the itemCount of the cart Your program must create an instance of the CashRegister...
Program must be in Python Write a program in Python whose inputs are three integers, and...
Program must be in Python Write a program in Python whose inputs are three integers, and whose output is the smallest of the three values. Input is 7 15 3
1. Write Python program that use a while loop to determine how long it takes for...
1. Write Python program that use a while loop to determine how long it takes for an investment to double at a given interest rate. The input will be an annualized interest rate, and the output is the number of years it takes an investment to double. Note: The amount of initial investment can be any positive value, you can use $1. 2. Write a function def DoubleInvest(initialInvest) to perform the same functionalities as question1. Make sure to include you...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Design a Python program with a While loop that lets the user enter a number. The...
Design a Python program with a While loop that lets the user enter a number. The number should be multiplied by 10 and the result stored in a variable named "product". The loop should repeat as long as the product variable < 10. Anything not completely obvious to a newbie should be commented on as in line comments. Should be modulated and repeatable as well as there should be an invocation of the main routine at the end of the...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the...
Write a PYTHON program that uses a while loop to prompt for 5 integers. If the number is equal to 99, display "Ninety-nine", otherwise display "no-match". If the number is equal to 75, display "Seventy-five", otherwise display "no-match".
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT