大家好,小编来为大家解答以下问题,高中python语言常用语句,高中python例题和答案,现在让我们一起来看看吧!
Source code download: 本文相关源码
一、选择结构程序设计
无( )与{ },用冒号:和缩进执行
【例1】
1、c++
#include<iostream>
using namespace std;
int main()
{
double x=0, total=0;
cin>>x;
if(x<2.5)
total=8.4*x;
else
total=7.6*x;
cout<<"共花费"<<total<<"元";
}
2、Python
注意:Python无double只有float
>>> x=float(input())
3
>>> if x<2.5:
... print("总价为:",8.4*x)
... else:
... print("总价为:",7.6*x)
...
总价为: 22.799999999999997
以上两种写法采用最简单的if-else样式均未考虑输入错误数据,如负