用分支结构实现分段函数计算,如下所示:当x<0时, y=0; 当0<=x<5时, y=x;当5<=x<10时, y=3x-5;当10<=x<20时, y=0.5x-2;当20<=x时, y=0
1.代码代码如下:x = int(input('请输入x的值:'))if x < 0:print('y=0')elif 0 <= x < 5:print('y=', x)elif 5 <= x < 10:print('y=', 3 * x - 5)elif 10 <= x < 20:print('y=', 0.5 * x - 2)else:print('y
·
1.代码
代码如下:
x = int(input('请输入x的值:'))
if x < 0:
print('y=0')
elif 0 <= x < 5:
print('y=', x)
elif 5 <= x < 10:
print('y=', 3 * x - 5)
elif 10 <= x < 20:
print('y=', 0.5 * x - 2)
else:
print('y=', 0)
2.运行结果
D:\Python代码\Scripts\python.exe C:/Users/Lenovo/PycharmProjects/pythonFirstStart/实验二/demo2.py
请输入x的值:5
y= 10
Process finished with exit code 0
更多推荐
已为社区贡献2条内容
所有评论(0)