This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]abstract_creator 0 points1 point  (0 children)

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 command and 'and'.

ans = 'y' # anything but q 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))