Friday, 15 May 2015

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


When I input first, second and last number, as well as the sum and average.

  #create function def list_sum (num_list): The amount of numbers for i in num_list print the_sum = 0: the_sum = the_sum + i the_sum #accept 3 and archive Input variable = input ("Input number:")) input_2 = float (raw_input ("input second number:")) input_3 = float (raw_input ("final number input:")) # list input list list_of_inputs = [input_1, input_2, input_3] # Calculate and sum of numbers sum_of_input = list_sum (list_of_inputs) Print ("sum: {: .2f}". Format (sum_of_input) Print and print the average of the numbers the_average = (sum_of_input) / (lane (list_of_inputs)) print ( "Aus D: {: .2f} ". Format (the_average) # Print percent of each percent and represents the percentage of total list_of_inputs for input_in_list: percent_total = input_in_list / sum_of_input print (" Total number of each number: {: .2f} ". Format (percent_tall))  

If I correctly describe your question I understand that, you can add white space only in logic within raw_input or print :

  #create function def list_sum ( Num_list ): # Num_list the number of numbers for I print the_sum = 0: the_sum = the_sum + i the_sum #accept 3 number and the storage Input variable = input ("input number:")) input_2 = float (raw_input ("input second Number: ")) input_3 = float (raw_input (" last number input: ")) # list inputs_off_input = [input_1, input_2, input_3] # numbers and the sum of numbers sum_of_input = list_sum (list_of_inputs) print (" sum: { : 6.2f} ". Print the format (sum_of_input) and Print Print Number (average: {: 6.2 F} ". Format (the_average)) # Print the percentage of total digits and print each number represents the input_in_list in List_of_inputs: Percent_total = input_in_list / sum_of_input print ("total number of each number: {: 6.2f}". Format (percentage_tall))  

output

 input number: 1 input A second number: 2 input final number: 3 amount: 6.00 average: 2.00 each Total percentage of the number: 0.17 Total percentages of each number: 0.33 percent of the total of each number: 0.50 

You have already worked with specifying 2 digits after decimals. For the format specifier Add some random total width (6), this width is the number of digits before decimal, decimal itself , and after the number of digits is included.


No comments:

Post a Comment