Starting from:

$25

METCSS521-Assignment 5 Solved

For this week we are adding some new style requirements.

Up to this point, we have deducted for:

•       Improper naming of programs or zip container

o   Among other things, names be all lower case

•       Missing program docstring

•       Inadequate # line comments

o   Just a few in each program - don't excessively comment

•       Going significantly over 80 characters for code/comment lines

•       Using CamelCase or anything other than lower case plus underscores for variables and function names. 

o   CONSTANT variables are the one exception and must be all upper case.

•    Asking for input() without telling the user what is expected.

o   Especially if asking for input that will be split(). Don’t make users have to check the code!

o   x = input('Enter two numbers:')   # Vague

•    Printing output that isn’t clearly explained (where necessary)

 

Starting with Homework 5, we will also be deducting for:

•    Not providing a docstrings for functions and class objects

•    Crashing uncontrolled on bad user input

•    Having too large a scope for try blocks

•    Not using format() to round output and add thousand separating commas (US standards)

o   123456.126  should be 123,456.13 è '{:,.2f}'.format(123456.126)

•    Having a “main()” function, with no input arguments or return values

·       Not using an if __name__ == '__main__' block where appropriate

 


 

 

Chapter 5 Exercises

 

 

5.5.1:  Write a python program that does the following: 

 

·     takes as input from the user an English sentence

·     calls the function vc_counter() that:

o   takes a string argument 

o   counts the number of vowels and consonants in the string

o   returns a dictionary of the counts, using the keys total_vowels and total_consonants

·     Uses the return from vc_counter() to print the total vowels and consonants with appropriate descriptions.

Example:

Enter an English sentence: The quick brown fox's jump landed past the lazy dog!
Total # of vowels in sentence: 12
Total # of consonants in sentence: 29

 

 


 

 

5.5.2:   Write a python program that does the following:

 

·       takes as input from the user a date and time (24-hour clock) as "MM/DD/YYYY HH:mm:SS"

o   Assume that everything must be provided with leading zeros

·       calls the function is_ validate_datetime () that:

o   takes a string argument

o   validates that the string has all the elements to be a valid date and time

o  returns 2 values:

§  Boolean true if valid or false if invalid date/time

§  None if valid or an error message string if invalid

·       If the input string is returned from the function as invalid, print the returned error message.

·       If the input string is returned from the function as valid, use input string to print following:

o  MM/DD/YYYY

o  HR:MIN:SEC

o  MM/YYYY

o  Whether the time is “AM” or “PM”

            

You must write your own algorithms. Do not use existing functions like datetime.strptime

 

Example 1:

            Enter a date time (MM/DD/YYYY HR:MIN:SEC): 12/13/2020 11:31:41
MM/DD/YYYY is 12/13/2020

      HR:MIN:SEC is 11:31:41

      MM/YYYY is 12/2020
The time is AM

 

Example 2:

            Enter a date time (MM/DD/YYYY HR:MIN:SEC): 122/04/1990 13:12:12
Invalid: there can be only 12 months in a year.

            Think of all possible erroneous inputs and write code to handle them. 






 

Chapter 6 Exercise

 

5.6.3     Write a python program that does the following:

Prompts the user for three numbers in one request.  
Be sure to specify the “delimiter” by which a user enters those three numbers. 

Divides the first number by the second number and add that result to the third number.

Prints output that shows in one line the formula applied and the result of the calculation.

Validate input by: 

•    Checking the user entered 3 values

•    Appropriately checking for the following errors: ValueError and ZeroDivisionError.  

•    Printing descriptive error messages to the console if validation fails.

·       Remembering to have very granular testing blocks



No Example Output provided for this question.





 

Chapter 8 Exercise

 

5.8.4:   Write a python program that does the following:

Prompt for a file name of text words. 
Words can be on many lines with multiple words per line.

Read the file and convert the words to a list. 

Call a function you created called list_to_words(), that takes a list as an argument and returns a list that contains only words that occurred once in the file.

Print the results of the function with an appropriate description.

Think about everything you must do when working with a file.



No Example Output provided for this question.





 

Chapter 15 Exercise

 

5.15.5: The formula for calculating the amount of money in a savings account that begins with an initial principal value (P) and earns annual interest (i) for (n) years is: P(1 + i)n
Note that i is a decimal, not a percent interest rate: .1 NOT 10%

 

            Write a python program that does the following:



·       Prompt the user on three lines for principal, percent interest rate and number of years to invest (using descriptive prompts)

o   Use a while loop to keep prompting until entries are valid

·       Call your function calc_compound_interest() that:

o   takes the arguments principle, int_rate, years

o   uses the above formula to calculate the ending value of the account

o   returns the value

·       Call a second function calc_compound_interest_recursive() that:

o   takes the arguments principle, int_rate, years

o   calculates the value recursively – calling a base calculation over and over instead of using the number of year as an exponent.

o   return that value

·       Print both values with clear descriptions and formatted with thousand commas and 2 decimal places. Then print whether the two values are equal when rounded to 4 decimal places.



More products