Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Python String Manipulation and File I/O The we have seen the print statement send output to the console using a string with format specifiers (the
Python String Manipulation and File I/O The we have seen the print statement send output to the console using a string with format specifiers (the % tokens in the string). We often put that string in our code as a literal. But, we can also build the string up, so that the format specifiers in it are custom to the values they will print out. The following code allows the user to enter any number of values and then prints them all out on one formatted line. ans = 'y' # anything but a UserVals = [] while (ans.lower() != 'q'): UserVals.append(float(input('Enter a value: '))) ans = input('Enter q to quit or anything else to continue: ') FormatStr = 'The values entered were: %5.2f' if ( len(UserVals) > 1) : for val in UserVals[1:-1]: FormatStr = FormatStr + ', %5.2f' FormatStr = FormatStr + ', and %5.2f.' else: # only one value entered FormatStr = FormatStr + '.' print (FormatStr % tuple(UserVals)) Understand a few things about this code. We are filling a string a bit at a time to get the formatting correct. This lets us account for the user entering values that should be displayed differently (commas, the 'and' conjunction, etc.) depending on how many were entered. The above code does not build the actual string that gets sent to the console. We could have also done that. The code above just builds the format string and then uses that for string interpolation with the list of user values, that we convert to a tuple for the interpolation. 1. Write a Python program that asks the user for a series of numbers, as in the example above. But, there are some improvements to be made over the example. For each number, assume that if the number has no fractional part that it is to be displayed as an integer. If it has a non-zero fractional part, then display it using a floating point format that has 2 digits to the right of the decimal and is otherwise is no longer than needed to print the number (that is, with no extra leading spaces). Print the user's numbers on one line with commas and 'and's as above. Several example runs are shown below. C:\Users\brokow\UCM Classes \ME 021\2020 Spring Assignments >HW05_01.py Enter a value: 3 Enter Q to quit or anything else to continue: 9 The values entered were: 3. ME021 Engineering Computing - Spring 2020 C:\Users\brokow\UCM Classes \ME 021\2020 Spring Assignments >HW05_01.py Enter a value: 3 Enter q to quit or anything else to continue: Enter a value: 4.673 Enter Q to quit or anything else to continue: Enter a value: 93.09 Enter q to quit or anything else to continue: 9 The values entered were: 3, 4.67, and 93.09. C:\Users\brokow\UCM Classes\ME 021\2020 Spring Assignments >HW05_01.py Enter a value: 943842.63 Enter q to quit or anything else to continue: Enter a value: 4 Enter Q to quit or anything else to continue: q The values entered were: 943842.63 and 4. C:\Users\brokow\UCM Classes \ME 021\2020 Spring Assignments >HW05_01.py Enter a value: 3.23 Enter Q to quit or anything else to continue: Enter a value: 8.3 Enter Q to quit or anything else to continue: Enter a value: 9.2834 Enter Q to quit or anything else to continue: 9 The values entered were: 3.23, 8.30, and 9.28
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started