当前位置:   article > 正文

python想右对齐_如何在python中右对齐我的答案?

python想右对齐_如何在python中右对齐我的答案?

如果我正确地理解了您的问题,您只需在参数中添加空格到raw_input或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))

输出

^{pr2}$

您已经完成了大部分工作,指定小数点后的2位数字。我还添加了一些随机的总宽度(6)到格式说明符。这个宽度包括小数点前的位数,小数点本身,以及后面的位数。在

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/294555
推荐阅读
相关标签
  

闽ICP备14008679号