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)到格式说明符。这个宽度包括小数点前的位数,小数点本身,以及后面的位数。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值