设计程序实现购物打折功能,要求如下: 1 输入需要购买的金额 2 判断:金额小于300时,没有折扣;金额在300~600(包括300,不包括600)时打九折;金额在600及以上时打8折 3 输出折扣后的金额
时间: 2023-07-15 19:13:22 浏览: 41
下面是一个Python实现的示例代码:
```python
price = float(input("请输入购买金额:"))
if price < 300:
discount = 1.0
elif price < 600:
discount = 0.9
else:
discount = 0.8
final_price = price * discount
print("折扣后的金额为:", final_price)
```
首先,程序通过`input`函数获取用户输入的购买金额,然后使用条件语句判断打折信息,最后计算折扣后的金额并输出。注意,需要把输入的字符串转换为浮点数类型,否则会导致计算错误。
相关问题
编写一个打折函数,功能是计算购买商品的折扣金额,要求折扣率可以输入
好的,我可以回答这个问题。以下是一个简单的打折函数的示例代码:
```c
#include <stdio.h>
float discount(float price, float rate) {
return price * rate;
}
int main() {
float price, rate;
printf("请输入商品价格:");
scanf("%f", &price);
printf("请输入折扣率:");
scanf("%f", &rate);
printf("折扣金额为:%.2f\n", discount(price, rate));
return ;
}
```
这个函数接受两个参数,商品价格和折扣率,返回折扣金额。在主函数中,我们通过 scanf 函数从用户输入中获取价格和折扣率,然后调用 discount 函数计算折扣金额并输出。
shop = {"洗衣液":20 ,"薯片":7.5 ,"卫生纸":24.99 ,"方便面":5 , # "笔记本":1.5,"烤鱼干": 5, "绿茶": 3, "柠檬茶": 3, # "牛奶": 4, "巧克力": 5,"螺蛳粉":15} # 实现以下功能: 1从控制台进行两次输入,分别输入要购买多项的物品x和数量y,并计算出总计消费z,最后输出“您购买了x件y,总计消费z元”shop = {"洗衣液":20 ,"薯片":7.5 ,"卫生纸":24.99 ,"方便面":5 , # "笔记本":1.5,"烤鱼干": 5, "绿茶": 3, "柠檬茶": 3, # "牛奶": 4, "巧克力": 5,"螺蛳粉":15} # 实现以下功能: (1)从控制台进行两次输入,分别输入要购买多项的物品x和数量y,并计算出总计消费z,最后输出“您购买了x件y,总计消费z元” (2)设计程序实现购物打折功能,要求如下: 1 输入需要购买的金额 2 判断:金额小于300时,没有折扣;金额在300~600(包括300,不包括600)时打九折;金额在600及以上时打8折 3 输出折扣后的金额
实现第一部分功能的代码:
```python
shop = {"洗衣液":20 ,"薯片":7.5 ,"卫生纸":24.99 ,"方便面":5 ,
"笔记本":1.5,"烤鱼干": 5, "绿茶": 3, "柠檬茶": 3,
"牛奶": 4, "巧克力": 5,"螺蛳粉":15}
x = input("请输入要购买的物品名称:")
y = int(input("请输入要购买的数量:"))
z = shop[x] * y
print("您购买了{}件{},总计消费{}元".format(y, x, z))
```
实现第二部分功能的代码:
```python
price = float(input("请输入购买金额:"))
if price < 300:
discount = 1
elif price < 600:
discount = 0.9
else:
discount = 0.8
discount_price = price * discount
print("折扣后的金额为:{}元".format(discount_price))
```
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)