alignment - How to right align my answers in python? -


how right align answers when input first, second , last number, sum , average.

#create function  def list_sum(num_list):     #calculate , print out sum of numbers     the_sum = 0     in num_list:         the_sum = the_sum +     return the_sum  #accept 3 numbers , store them in variables input_1 = float(raw_input("input number: "))  input_2 = float(raw_input("input second number: "))  input_3 = float(raw_input("input last number: "))    #take list of inputs list_of_inputs = [input_1, input_2, input_3]  #calculate , print out sum of numbers sum_of_input = list_sum(list_of_inputs) print("the sum: {:.2f}".format(sum_of_input))  #calculate , print out average of numbers the_average = (sum_of_input)/(len(list_of_inputs)) print("the average: {:.2f}".format(the_average))  #calculate , print out percent of total each number represents input_in_list in list_of_inputs:     percent_total = input_in_list/sum_of_input     print("the percent of total of each number: {:.2f}".format(percent_total)) 

if understand question correctly, can add whitespace inside argument raw_input or print:

#create function def list_sum(num_list):     #calculate , print out sum of numbers     the_sum = 0     in num_list:         the_sum = the_sum +     return the_sum  #accept 3 numbers , store them in variables input_1 = float(raw_input("input number:        ")) input_2 = float(raw_input("input second number: ")) input_3 = float(raw_input("input last number: "))  #take list of inputs list_of_inputs = [input_1, input_2, input_3]  #calculate , print out sum of numbers sum_of_input = list_sum(list_of_inputs) print("the sum:     {:6.2f}".format(sum_of_input))  #calculate , print out average of numbers the_average = (sum_of_input)/(len(list_of_inputs)) print("the average: {:6.2f}".format(the_average))  #calculate , print out percent of total each number represents input_in_list in list_of_inputs:     percent_total = input_in_list/sum_of_input     print("the percent of total of each number: {:6.2f}".format(percent_total)) 

output

 input number:        1 input second number: 2 input last number: 3 sum:       6.00 average:   2.00 percent of total of each number:   0.17 percent of total of each number:   0.33 percent of total of each number:   0.50 

you did of work specifying 2 digits following decimal. added random total width (6) format specifier. width includes number of digits before decimal, the decimal itself, , number of digits after.


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -