Reference no: EM132356908
Assignment
Purpose:
To allow you to exercise with a logical thinking process to formulate algorithms, and to implement the algorithms using the Python Programming Language. The logic will include simple inputs and outputs, looping, using counters, conditional statements (if and else), and basic functions. This is an individual assignment.
Questions:
Write a separate Python program for each of the following:
a. Ask the user to input from the keyboard a positive non-zero number. You are to validate that the input is indeed as expected. Each time the user inputs an invalid input, you will give them an error message, and ask them to input another value. Impose a limit of three wrong attempts for the inputs, after which you quit the program. Any number starting with the +sign is also accepted. Decimal numbers cannot start with `.1, and cannot end with `.1. If the three wrong attempts have been exhausted, print to the screen a message indicating that, otherwise print correct input.
To validate the input, you must create a function for this problem called isValid(x) that takes a number x, and returns a true or false indicating whether the number x is valid or not.
b. Ask the user to input from the keyboard a string composed of the characters a, b, or c. Your input is case sensitive (a's are different than A's as an example). However, the user may decide to input a string with invalid characters. Your program will output to the screen the number of a's in the string, the number of b's, the number of c's, as well as the number of all other invalid characters (non a, b, or c). This means that you will not prohibit the user from inputting a string composed of other characters also.
c. The objective is for you to print all the numbers divisible by a number n in the range [start, end]. You are to ask the user to input through from the keyboard integer numbers (+ and - signs are accepted before the number):
i. A variable start to indicate the start of the range.
ii. A variable end to indicate the end of the range, and
iii. A variable n to indicate the number to check divisibility for.
For example, if the user inputs 1000 for start, 3500 for end, and 5, your program will print all the numbers divisible by 5 in [1000,3500]. You
must validate that start, end, n are valid integers. If an input is invalid, you need to keep on asking the user for a valid input without any limitation on how many times this can happen.
d. Let the user input ten numbers from the keyboard, and find and print their maximum. (Hint: use a loop, start by assuming maximum to be the first one that is input. Each time you find a number greater than max, make it the max, then at the end, print it out). You cannot use the max function in Python for this exercise. You cannot sort either. Assume that the input is proper (no need for validation).
e. Input five numbers from the keyboard, compute and print their standard deviation. The standard deviation is given as:
σ = √(1/N ΣNi=1(xi-μ)2)
To compute the standard deviation, you are to:
i. Compute the average of the five numbers.
ii. Subtract the average from each number, then square the result, call this quantity the square of the difference.
iii. Sum up all of the square of the differences.
iv. Divide the square of the differences by 5.
v. Compute the square root of the value in (iv).
vi. Print the result.
Make sure each of the above steps is performed properly before you proceed with the next one. Assume that the numbers are all valid (no
need for validation).