diff --git a/app2.py b/app2.py new file mode 100644 index 000000000..289dd7c0c --- /dev/null +++ b/app2.py @@ -0,0 +1,12 @@ +from flask import Flask, jsonify +import random + +app = Flask(__name__) + +@app.route('/random', methods=['GET']) +def random_number(): + number = random.randint(1, 100) + return jsonify({'random_number': number}) + +if __name__ == '__main__': + app.run(debug=True) diff --git a/dict.py b/dict.py new file mode 100644 index 000000000..475348769 --- /dev/null +++ b/dict.py @@ -0,0 +1,43 @@ +# 创建一个空字典 +fruits = {} + +# 创建一个包含一些水果及其数量的字典 +fruits = { + 'apple': 10, + 'banana': 5, + 'orange': 8 +} +# 访问字典中的值 +print(fruits['apple']) # 输出: 10 +# 添加一个新的键值对 +fruits['mango'] = 15 + +# 更新一个已有的键值对 +fruits['banana'] = 7 + +print(fruits) +# 输出: {'apple': 10, 'banana': 7, 'orange': 8, 'mango': 15} +# 删除一个键值对 +del fruits['orange'] + +print(fruits) +# 输出: {'apple': 10, 'banana': 7, 'mango': 15} + +# 遍历字典中的键值对 +for fruit, quantity in fruits.items(): + print(f"The quantity of {fruit} is {quantity}") + +# 检查键是否在字典中 +if 'apple' in fruits: + print("Apple is in the dictionary") + +if 'grape' not in fruits: + print("Grape is not in the dictionary") + +# 获取所有的键 +keys = fruits.keys() +print(keys) # 输出: dict_keys(['apple', 'banana', 'mango']) + +# 获取所有的值 +values = fruits.values() +print(values) # 输出: dict_values([10, 7, 15]) \ No newline at end of file diff --git a/students.json b/students.json new file mode 100644 index 000000000..67995e031 --- /dev/null +++ b/students.json @@ -0,0 +1,18 @@ +[ + { + "name": "Alice", + "age": 23, + "courses": [ + "Math", + "Science" + ] + }, + { + "name": "Charlie", + "age": 24, + "courses": [ + "Art", + "Design" + ] + } +] \ No newline at end of file diff --git a/students.py b/students.py new file mode 100644 index 000000000..87775b9f7 --- /dev/null +++ b/students.py @@ -0,0 +1,39 @@ +import json + +# 读取 JSON 文件 +with open('students.json', 'r') as file: + students = json.load(file) + +print(students) + +# 遍历学生列表 +for student in students: + name = student['name'] + courses = student['courses'] + print(f"Student: {name}") + print("Courses:") + for course in courses: + print(f" - {course}") + +# 添加新学生 +new_student = { + "name": "Charlie", + "age": 24, + "courses": ["Art", "Design"] +} +students.append(new_student) + +# 将更新后的数据写回到 JSON 文件 +with open('students.json', 'w') as file: + json.dump(students, file, indent=4) + +print("New student added.") + +# 删除名为 "Bob" 的学生 +students = [student for student in students if student['name'] != 'Bob'] + +# 将更新后的数据写回到 JSON 文件 +with open('students.json', 'w') as file: + json.dump(students, file, indent=4) + +print("Student 'Bob' deleted.") \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 7bee67b55..91458ae75 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,7 +7,7 @@

- GitHub Codespaces ♥️ Flask + GitHub Codespaces ♥️hello wulb Flask

Edit templates/index.html and refresh to see your changes!