Question

How much money do you think you would earn in a period of 30 days if...

How much money do you think you would earn in a period of 30 days if you were paid as follows: one cent for the first day, two cents for the second day, four cents for the third day, eight cents for the fourth day, and so on (i.e. your salary doubles each day)? Do you think you would make very little money, just a few dollars, at the end of the 30-day period? Let us write a program to find out!

Details:

  • The program should ask the user for a number of days. This is an integer value, and it must be a value greater than zero.
  • The program must validate the input given by the user and make sure that:
    • if the user enters a non-integer the program issues an error message and requests the user provides a valid input
    • if the user enters a number that is zero or less than zero the program alerts the user of the mistake and requests the user enter a valid number.
    • validation must be done using loops.
  • The program should display a table showing what the earnings are for each day, and then show the total pay accrued by the end of the period. The output should be displayed in a dollar amount, not the number of cents.
    • use either format() or the string formatters/conversion specifiers to display the table and amounts of money with 2 decimal places.

Here is a sample run:

Enter the number of days: 10

Day  Pennies
1    $0.01
2    $0.02
3    $0.04
4    $0.08
5    $0.16
6    $0.32
7    $0.64
8    $1.28
9    $2.56
10   $5.12

The total salary for 10 days is $10.23

Notes:

  • The purpose of this problem is to practice using loops and try/except.
  • Please make sure to submit a well-written program. Good identifier names, useful comments, and spacing will be some of the criteria that will be used when grading this assignment.
  • This assignment can be and must be solved using only the materials that have been discussed in class. Do not look for alternative methods that have not been covered as part of this course.

How your program will be graded:

  • correctness: the program validates input and performs calculations correctly: 40%
  • complies with requirements (it properly uses loops, try/except): 40%
  • code style: good variable names, comments, proper indentation and spacing : 20%

Language = python 3

Homework Answers

Answer #1
n=0
#loop until user gives valid input
while(True):
    try:
      n=int(input("Enter number of days: "))
      if(n>0):
        break
      else:
        print("Number of days must be greater than 0. Try again")
    except:
      print("Invalid input. Try again")

print("Day\tPennies")
total=1
print("1\t$"+str(round(total/100,2)))
#iterating for given days and printing the salary
for i in range(2,n+1):
    total=total + total        
    print(str(i)+"\t$"+str(round(total/100,2)))
print("Total salary for ",n," days is $"+str(round(total/100,2)))

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Design a program that calculates the amount of money a person would earn over a period...
Design a program that calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing what salary was for each day, and then show the total pay at the end of the period. The output should be displayed in a...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
Now we want to display which error was thrown in our voting program. ( BOTH QUESTIONS...
Now we want to display which error was thrown in our voting program. ( BOTH QUESTIONS ARE PYTHONS PROGRAMS) Add the appropriate code to the try/except statement so the exception is displayed. Run the program and, when prompted, enter the word 'old' so your output matches the output under Desired Output. # Set the variable age = input("What is your age?") # Insert a try/except statement # here when you convert the input # to a number using int() try:...
In this exercise we will practice using loops to handle collections/containers. Your job is to write...
In this exercise we will practice using loops to handle collections/containers. Your job is to write a program that asks the user to enter a sentence, then it counts and displays the occurrence of each letter. Note: your program should count the letters without regard to case. For example, A and a are counted as the same. Here is a sample run: Enter a sentence: It's a very nice day today! a: 3 times c: 1 times d: 2 times...
Write some C++ program segments that solves the indicated tasks (you do not have to write...
Write some C++ program segments that solves the indicated tasks (you do not have to write a complete program, nor be concerned about "good" output; a small code segment will be enough). a) A program that gets a double number from the user, decides whether that number is positive, negative, or zero and display its decision on the screen. a) A function isPositive that takes as input a double number and returns the integer 1 if the number is positive,...
Description: The purpose of this assignment is to practice writing code that calls functions, and contains...
Description: The purpose of this assignment is to practice writing code that calls functions, and contains loops and branches. You will create a C program that prints a menu and takes user choices as input. The user will make choices regarding different "geometric shapes" that will be printed to the screen. General Comments: Your code must contain at least one of all of the following control types: nested for() loops a while() or a do-while() loop a switch() statement an...
Write a program (C language) that will read the number of a month and will print...
Write a program (C language) that will read the number of a month and will print the number of days in the month. Ignore leap years. Use 28 days for February. Have the program runs in a continuous loop, allowing the user to enter a month number, see that number of days, and repeat. Use month number = 0 to exit the loop and the program. Program must meet the following criteria: 1.Your name and the name of the program...
Python code only! (Do not include breaks or continue functions) Write a program that asks the...
Python code only! (Do not include breaks or continue functions) Write a program that asks the user to enter the amount that they budgeted for the month. A loop should then prompt the user to enter their expenses, one at a time and to enter 0 to quit. When the loop finishes, the program should display the the amount of budget left. (a negative number indicates the user is over budget and a positive number indicates the user is under...
Using python, write the program below. Program Specifications: You are to write the source code and...
Using python, write the program below. Program Specifications: You are to write the source code and design tool for the following specification: A student enters an assignment score (as a floating-point value). The assignment score must be between 0 and 100. The program will enforce the domain of an assignment score. Once the score has been validated, the program will display the score followed by the appropriate letter grade (assume a 10-point grading scale). The score will be displayed as...
You must use Windows Programming(Do NOT use Console) to complete this assignment. Use any C# method...
You must use Windows Programming(Do NOT use Console) to complete this assignment. Use any C# method you already know. Create a Windows application that function like a banking account register. Separate the business logic from the presentation layer. The graphical user interface should allow user to input the account name, number, and balance. Provide TextBox objects for withdrawals and deposits. A button object should be available for clicking to process withdrawal and deposit transactions showing the new balance. Document and...