某商品出售按购买数量打折,如果购买数量超过30个则按95折销售,输 入商品数量和商品单价(元),输出所购商品的总价,写一个程序
时间: 2023-05-24 07:03:40 浏览: 61
。A:
```python
quantity = int(input("请输入商品数量:"))
price = float(input("请输入商品单价(元):"))
if quantity > 30:
total_price = quantity * price * 0.95
else:
total_price = quantity * price
print("所购商品的总价为:{:.2f}元".format(total_price))
```
注释:
1. 通过 `input` 函数获取用户输入的数量和单价,分别将其转换为整型和浮点型。
2. 使用 `if` 语句判断购买数量是否超过30个,如果是,则打95折,否则按照原价计算。
3. 通过格式化输出的方式将总价保留两位小数并显示。
相关问题
某商品出售按购买数量打折,如果购买数量超过30个则按95折销售,输 入商品数量和商品单价(元),输出所购商品的总价写一个程序
。A:
```python
amount = int(input("请输入商品数量:"))
price = float(input("请输入商品单价(元):"))
total = amount * price
if amount > 30:
total *= 0.95
print("总价为:%.2f 元" % total)
```
首先通过 `input` 函数获取用户输入的商品数量和单价,然后计算商品的总价并赋值给变量 `total`。
接着通过 `if` 语句判断购买数量是否超过30个,如果超过则将总价乘以0.95,相当于打95折。
最后使用字符串格式化输出结果。`\%.2f` 表示输出的是一个浮点数,并且保留两位小数。
⑴某商品出售按购买数量打折,如果购买数量超过30个则按95折销售,输入商品数量和商品单价(元),输出所购商品的总价
好的,根据您提供的信息,我给出以下程序代码:
```python
num = int(input("请输入购买数量:"))
price = float(input("请输入商品单价(元):"))
if num > 30:
total = num * price * 0.95
else:
total = num * price
print("所购商品的总价为:%.2f元" % total)
```
如果您购买数量不超过30个,则按照单价计算总价;如果购买数量超过30个,则按照95折的价格计算总价。程序会输出所购商品的总价,保留两位小数。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)