-1

How do I right align my answers when I input the first, second and last number, as well as the sum and the average.

#create function 
def list_sum(num_list):
    #calculate and print out the sum of numbers
    the_sum = 0
    for i in num_list:
        the_sum = the_sum + i
    return the_sum

#accept 3 numbers and store them in variables
input_1 = float(raw_input("Input a number: ")) 
input_2 = float(raw_input("Input a second number: ")) 
input_3 = float(raw_input("Input the last number: "))  

#take list of inputs
list_of_inputs = [input_1, input_2, input_3]

#calculate and print out the sum of numbers
sum_of_input = list_sum(list_of_inputs)
print("The sum: {:.2f}".format(sum_of_input))

#calculate and print out the average of the numbers
the_average = (sum_of_input)/(len(list_of_inputs))
print("The average: {:.2f}".format(the_average))

#calculate and print out the percent of the total that each number represents
for input_in_list in list_of_inputs:
    percent_total = input_in_list/sum_of_input
    print("The percent of the total of each number: {:.2f}".format(percent_total))
zschleien
  • 9
  • 1
  • 6
  • instead of immediately printing each answer. save each into an array as strings, get the length of the longest string and then pad the start of each of the other strings to be the same length as the longest. Then print. – Calum Mar 16 '15 at 21:30
  • Maybe this post will help: http://stackoverflow.com/questions/8234445/python-format-output-string-right-alignment – user985030 Mar 16 '15 at 21:32
  • do you want all output aligned or each particular output aligned? – Padraic Cunningham Mar 16 '15 at 21:34

1 Answers1

0

If I understand your question correctly, you can just add whitespace inside the argument to raw_input or print:

#create function
def list_sum(num_list):
    #calculate and print out the sum of numbers
    the_sum = 0
    for i in num_list:
        the_sum = the_sum + i
    return the_sum

#accept 3 numbers and store them in variables
input_1 = float(raw_input("Input a number:        "))
input_2 = float(raw_input("Input a second number: "))
input_3 = float(raw_input("Input the last number: "))

#take list of inputs
list_of_inputs = [input_1, input_2, input_3]

#calculate and print out the sum of numbers
sum_of_input = list_sum(list_of_inputs)
print("The sum:     {:6.2f}".format(sum_of_input))

#calculate and print out the average of the numbers
the_average = (sum_of_input)/(len(list_of_inputs))
print("The average: {:6.2f}".format(the_average))

#calculate and print out the percent of the total that each number represents
for input_in_list in list_of_inputs:
    percent_total = input_in_list/sum_of_input
    print("The percent of the total of each number: {:6.2f}".format(percent_total))

Output

Input a number:        1
Input a second number: 2
Input the last number: 3
The sum:       6.00
The average:   2.00
The percent of the total of each number:   0.17
The percent of the total of each number:   0.33
The percent of the total of each number:   0.50

You already did most of the work specifying 2 digits following the decimal. I also added some random total width (6) to the format specifier. This width includes the number of digits before the decimal, the decimal itself, and the number of digits after.

jedwards
  • 29,432
  • 3
  • 65
  • 92
  • 2
    As I noted in the preface, It's certainly possible I misunderstood the question -- care to elaborate downvoter? – jedwards Mar 16 '15 at 21:31