本题目要求计算下列分段函数f(x)的值:python
输入在一行中给出实数x。函数
在一行中按“f(x) = result”的格式输出,其中x与result都保留一位小数。spa
代码以下:code
#!/usr/bin/python
# -*- coding: utf-8 -*-
#计算f(x)
def f(m):
if m ==0:
result = 0
else:
result = 1/m
return result
m = float(input())
print("f({0:.1f}) = {1:.1f}".format(m,f(m)))
这里要求输出f(x) = resultorm
若是写成以下代码blog
print("f(",m,") =",f(m)) utf-8
则输出时显示为f( m ) = result,m的左右会多出一个空格来,不达要求。字符串
所以这里使用了format函数格式化字符串,format的用法不少,这里不细说,只说这两个表明的意思。input
{0:.1f} 输出下标为为0的字符,保留1位小数。form
{1:.1f}输出下标为1的字符,保留1位小数。
读书和健身总有一个在路上
原文:https://www.cnblogs.com/bbcdc12/p/14120932.html