Skip to content

add app2 dict #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app2.py
Original file line number Diff line number Diff line change
@@ -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)
43 changes: 43 additions & 0 deletions dict.py
Original file line number Diff line number Diff line change
@@ -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])
18 changes: 18 additions & 0 deletions students.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"name": "Alice",
"age": 23,
"courses": [
"Math",
"Science"
]
},
{
"name": "Charlie",
"age": 24,
"courses": [
"Art",
"Design"
]
}
]
39 changes: 39 additions & 0 deletions students.py
Original file line number Diff line number Diff line change
@@ -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.")
2 changes: 1 addition & 1 deletion templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<header class="App-header">
<img src="{{ url_for('static', filename='Octocat.png') }}" class="App-logo" alt="logo" />
<p>
GitHub Codespaces <span class="heart">♥</span> Flask
GitHub Codespaces <span class="heart">♥️hello wulb</span> Flask
</p>
<p class="small">
Edit <code>templates/index.html</code> and refresh to see your changes!
Expand Down