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"]
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
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...
Python Programming Question: Objective: Sum of Numbers. Design a program with a loop that asks the...
Python Programming Question: Objective: Sum of Numbers. Design a program with a loop that asks the user to enter a series of positive numbers. The user should enter "0" (zero) to signal the end of the series. After all the positive numbers have been entered, the program should display their sum. The program should display the following: • Contain proper indentation. • Comments (Blocks or Lines). • Good Variables initializations. • Good questions asking for the parameters of the code....
Create a python program that contains a while loop together with a Sentinel (0) to process...
Create a python program that contains a while loop together with a Sentinel (0) to process indefinite item costs that are purchased online from a vendor. Be sure that you assign a variable SENTINEL to 0 to use in the Boolean condition of your while loop. A sales tax rate of 6.25% is applied to the subtotal for the items purchased. Be sure you assign a variable, TAXRATE to 0.0625. The program is to process a number of items, numItems,...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that...
Complete the following in syntactically correct Python code. Write a program, using a for loop, that calculates the amount of money a person would earn over a period of time if his or her salary is 1 penny for the first day, 2 pennies for the second day, 4 pennies for the third day, and continues to double each day. 1.      The program should ask the user for the number of days the employee worked. 2.      Display a table showing the salary...
must use python Write a nested for loop that displays the following output using the same...
must use python Write a nested for loop that displays the following output using the same integer from part a:                                                     1                                                 1   2   1                                             1   2   4   2   1                                         1   2   4   8   4   2   1                                     1   2   4   8 16   8   4   2   1                                 1   2   4   8 16 32 16   8   4   2   1                             1   2   4   8 16 32 64 32 16   8   4   2   1                         1   2   4  ...
This program should be done in python. This must use the principles of object oriented program....
This program should be done in python. This must use the principles of object oriented program. Create one or more classes to play Four-in-a-Row (also called Connect Four) with a user. It’s similar to tic-tac-toe but the board is of size 7×6 and discs fall straight through so the legal moves are more stringent than tic-tac-toe. The state of the board should be printed to the terminal after each legal move. You can represent the different colored discs as X’s...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT