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 输出折扣后的金额
时间: 2023-07-02 11:03:51 浏览: 54
实现第一部分功能的代码:
```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))
```
相关问题
编写spark生成器生成字段title: String, author: String, price: Double, sales: Int, rating: Double, publishTime: String
这里是一个简单的 Spark 数据生成器,可以生成包含 `title`、`author`、`price`、`sales`、`rating` 和 `publishTime` 字段的数据。
```scala
import org.apache.spark.sql.{DataFrame, Row, SparkSession}
import java.sql.Timestamp
import scala.util.Random
object DataGenerator {
def generateData(spark: SparkSession, numRecords: Int): DataFrame = {
val titles = Seq("The Great Gatsby", "To Kill a Mockingbird", "1984", "Pride and Prejudice", "The Catcher in the Rye", "The Hobbit", "The Lord of the Rings", "Animal Farm", "The Diary of a Young Girl", "The Hitchhiker's Guide to the Galaxy")
val authors = Seq("F. Scott Fitzgerald", "Harper Lee", "George Orwell", "Jane Austen", "J.D. Salinger", "J.R.R. Tolkien", "George Orwell", "J.R.R. Tolkien", "Anne Frank", "Douglas Adams")
val prices = Seq(9.99, 12.99, 14.99, 19.99, 24.99, 29.99, 34.99, 39.99, 44.99, 49.99)
val sales = Seq(100, 200, 300, 400, 500, 600, 700, 800, 900, 1000)
val ratings = Seq(3.5, 4.0, 4.5, 5.0)
val dates = Seq("2021-01-01", "2021-02-01", "2021-03-01", "2021-04-01", "2021-05-01", "2021-06-01", "2021-07-01", "2021-08-01", "2021-09-01", "2021-10-01")
val random = new Random()
val data = (1 to numRecords).map { i =>
val title = titles(random.nextInt(titles.length))
val author = authors(random.nextInt(authors.length))
val price = prices(random.nextInt(prices.length))
val sale = sales(random.nextInt(sales.length))
val rating = ratings(random.nextInt(ratings.length))
val date = Timestamp.valueOf(dates(random.nextInt(dates.length)) + s" ${random.nextInt(24)}:${random.nextInt(60)}:${random.nextInt(60)}")
Row(title, author, price, sale, rating, date.toString)
}
val schema = spark.sqlContext.createDataFrame(Seq.empty[Row]).schema
.add("title", "string")
.add("author", "string")
.add("price", "double")
.add("sales", "int")
.add("rating", "double")
.add("publishTime", "string")
spark.createDataFrame(data, schema)
}
}
```
调用 `generateData` 函数即可生成指定数量的数据,例如:
```scala
val numRecords = 1000
val data = DataGenerator.generateData(spark, numRecords)
data.show()
```
请用python实现拟合第一段水位>> t=[0 0.92 1.84 2.95 3.87 4.98 5.9 7.01 7.93 8.97 10.95 12.03 12.95 13.88 14.98 15.90 16.83 17.93 19.04 19.9 20.84 23.88 24.99 25.91];h=[968 948 931 913 898 881 869 852 839 822 1082 1050 1021 994 965 941 918 892 866 843 822 1059 1035 1018];>> c1=polyfit(t(1:10),h(1:10),3)a1=polyder(c1)c1 = -0.0785 1.3586 -22.1079 967.7356a1 = -0.2356 2.7173 -22.1079%其中a1系数为流量函数系数>> tp1=0:0.1:9;x1=-polyval(a1,tp1);>> plot(tp1,x1,'c.')>> axis([0 25 12 34])xlabel('小时')ylabel('厘米/小时')>> hold on拟合第二段水位>> t=[0 0.92 1.84 2.95 3.87 4.98 5.9 7.01 7.93 8.97 10.95 12.03 12.95 13.88 14.98 15.90 16.83 17.93 19.04 19.9 20.84 23.88 24.99 25.91];h=[968 948 931 913 898 881 869 852 839 822 1082 1050 1021 994 965 941 918 892 866 843 822 1059 1035 1018];>> c2=polyfit(t(10.9:21),h(10.9:21),4)a2=polyder(c2)c2 = 1.0e+03 * -0.0000 0.0003 -0.0044 -0.0018 1.3644a2 = -0.0186 0.7529 -8.7512 -1.8313%其中a2系数为流量函数系数>> tp2=10.9:0.1:21;x2=-polyval(a2,tp2);plot(tp2,x2,'c.')>> hold on
以下是Python代码实现:
```
import numpy as np
import matplotlib.pyplot as plt
# 数据
t = np.array([0, 0.92, 1.84, 2.95, 3.87, 4.98, 5.9, 7.01, 7.93, 8.97, 10.95, 12.03, 12.95, 13.88, 14.98, 15.90, 16.83, 17.93, 19.04, 19.9, 20.84, 23.88, 24.99, 25.91])
h = np.array([968, 948, 931, 913, 898, 881, 869, 852, 839, 822, 1082, 1050, 1021, 994, 965, 941, 918, 892, 866, 843, 822, 1059, 1035, 1018])
# 第一段拟合
c1 = np.polyfit(t[:10], h[:10], 3)
a1 = np.polyder(c1)
tp1 = np.arange(0, 9.1, 0.1)
x1 = -np.polyval(a1, tp1)
plt.plot(tp1, x1, 'c.')
plt.axis([0, 25, 12, 34])
# 第二段拟合
c2 = np.polyfit(t[10:21], h[10:21], 4)
a2 = np.polyder(c2)
tp2 = np.arange(10.9, 21.1, 0.1)
x2 = -np.polyval(a2, tp2)
plt.plot(tp2, x2, 'c.')
plt.xlabel('小时')
plt.ylabel('厘米/小时')
plt.show()
```
注意,这里的`polyder`函数用于求解多项式的导数,因此需要使用NumPy科学计算库。此外,需要注意上述代码中的数组索引是从0开始的,因此`t[:10]`表示取t数组的前10个元素,而`t[10:21]`表示取t数组的第11个到第21个元素。
相关推荐
![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)