CS‐521 Homework Assignment 2


Category: You will Instantly receive a download link for .zip solution file upon Payment


5/5 - (6 votes)

Programming Problems 2.1: Write a Python program does all the following steps. Make sure you label all your print output to explain what you are printing. This is required for all problems in this course. a. Prompts the user to enter a whole number from 1 to 7.   No validation required for this problem. b. In one line of code/calculation, performs the following operations on the user supplied number in this exact order: o Multiply by 2 o Add 10 o Divide by 2 o Subtract the user supplied number c. Print the output of this calculation in part ‘b’ as an integer (no decimal places). d. Now take the same user supplied number from part ‘a’ and convert it to a three‐digit number with incrementing digits. Assign this number to a variable as an integer. o For example, if the user entered ‘3’, the three‐digit version is 345. e. Add the three digits together and print the results. o For example, if the user entered 3, you would calculate 3+4+5 = 12. f. Divide the three‐digit version by the resulting sum of its digits and print the results as a float. o For example, if the user entered 3, you would calculate 345 / 12. g. Reprint the output of part ‘f’ as a truncated integer (no decimal places). 2.2: Write a program that does the following: a. Prompt the user to enter a string or a number. b. Print that input 3 times, as (1) a string, (2) an integer, and (3) a floating‐point value.   o Based on the user input, this program will crash sometimes! c. What data types can be input that will print without generating any errors? o Answer this question at the end of your code by using a docscring (triple quote) comment and explain why for your answer makes sense for all three data types listed in part b.     CS‐521 Homework Assignment 2 3 2.3: Write a program that does the following: a. Prompt the user to enter a number No validation required for this problem. b. Take that number (n) and compute the value of n cubed, divided by n c. Print the formula and results, replacing the ‘n’ variables with the user input.   o Limit result to 2 decimal places   Example Output: Please enter a number: 8 8**3/8 = ###.## (you will fill in ###.## with the correct number) 2.4: Write a three‐line program (3 commands) that will a. prompt for a number b. convert the input to an integer c. print the number 0 if the user input is even and the number 1 if the user input is odd  Hint: One way to determine whether an integer is even or odd is to divide the number by two and check the remainder. Additional Rule: You can NOT use an ‘if statement’ in this program.     CS‐521 Homework Assignment 2 4 2.5: One of the most common beginning programming problems is the fizz‐buzz challenge.   Write a Python program as follows: a. After the program docstring, declare a constant variable named MAXVAL with a value of 30,   b. Create a for loop that will loop through all values from 1 to 30, using MAXVAL to help define the end value of the for loop.   c. For each number in the loop:    If the number is divisible by 2, print the word foo  If the number is divisible by 3, print the word bar  If the number is divisible by 5, print the word baz  If the number is divisible by more than one of these, print the combination on the same line.    If the number is not divisible by 2,3 or 5, do not print a string  Print the output of each number in the loop on a single line as  n: For example:  15 (3 & 5) would print  15: barbaz  6 (2 & 3) would print  6: foobar  30 (2 & 3 & 5) would print  30: foobarbaz  7 would print  7: d. After part ‘c’ is completed, print a separator line. e. Repeat step ‘c’ using a while loop from 1 to 30 (using the same MAXVAL constant to define the end of the range). Note: Your output must present these in the correct order (foo first, then bar, then baz). Hint: The modulo operator will help you out with this! Where to submit?   Click Assignments in the Navigation Area and then click on the title of the assignment to enter the submission area and upload your response.