编写程序,实现以下功能: (1) 创建空宇典die. student (2)由用户依次输入三名学生的姓名和年龄,存入宁典dic. student(3)输出宇典dic student中的内容. 格式为: 王建 18 张云 19 张秋兩 18
时间: 2024-02-20 19:56:40 浏览: 229
好的,以下是Python的代码实现:
```python
# 创建空字典
dic_student = {}
# 由用户输入三名学生的姓名和年龄,并存入字典
for i in range(3):
name = input("请输入学生姓名:")
age = input("请输入学生年龄:")
dic_student[name] = age
# 输出字典内容
for name, age in dic_student.items():
print(name, age, end=" ")
```
运行程序后,会依次让用户输入三名学生的姓名和年龄,然后将这些信息存入字典中,并最终按照指定格式输出字典中的内容。
相关问题
创建空字典die_student。 (2)由用户依次输入五名学生的姓名、年龄、身高和依联、依入典 die_student。将姓名作为健。年龄、身高和体重作为值。 C3)输出字典die_student中的内容。格式为 张秋雨18岁 刘欢17岁王建18岁张云19岁姜宇 东61 178cm 82kg 169cm 75kg 172cm 80kg 165cm55kg 170cm 70kg 提示:值是可变的。因此年龄、身高和体重可考出便用列战*源示。省 入输出话句已给出。请完善代码。实现功能
```python
# 创建空字典
die_student = {}
# 依次输入五名学生的信息
for i in range(5):
name = input("请输入第{}个学生的姓名:".format(i+1))
age = input("请输入第{}个学生的年龄:".format(i+1))
height = input("请输入第{}个学生的身高:")
weight = input("请输入第{}个学生的体重:")
# 将信息加入字典
die_student[name] = [age, height, weight]
# 输出字典中的内容
for name, info in die_student.items():
print("{}{}岁{}cm{}kg".format(name, info[0], info[1], info[2]))
```
注意:这里将年龄、身高和体重作为一个列表作为字典的值,方便输出时的格式化处理。
1.创建表t_student,其中包含学号stuno,姓名stuname,性别stusex三个字段,在表中插入一些测试数据。(5分) 2.实现下述功能: 编写显示所有学生资料信息页面,其中包含一个表单用来根据学生姓名模糊查询匹配的
答案如下:
1. SQL语句如下:
```
CREATE TABLE t_student (
stuno INT PRIMARY KEY,
stuname VARCHAR(20),
stusex VARCHAR(2)
);
INSERT INTO t_student VALUES
(1, '张三', '男'),
(2, '李四', '女'),
(3, '王五', '男'),
(4, '赵六', '女'),
(5, '钱七', '男');
```
2. 实现功能的代码如下:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>学生信息管理系统</title>
</head>
<body>
<h1>学生信息管理系统</h1>
<form action="search.php" method="get">
<label for="name">请输入学生姓名:</label>
<input type="text" name="name" id="name" placeholder="请输入学生姓名">
<input type="submit" value="查询">
</form>
<br>
<table border="1">
<tr>
<th>学号</th>
<th>姓名</th>
<th>性别</th>
</tr>
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "root", "password", "test");
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
// 查询学生信息
$sql = "SELECT * FROM t_student";
$result = mysqli_query($conn, $sql);
// 输出学生信息
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["stuno"] . "</td>";
echo "<td>" . $row["stuname"] . "</td>";
echo "<td>" . $row["stusex"] . "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='3'>暂无学生信息</td></tr>";
}
// 关闭数据库连接
mysqli_close($conn);
?>
</table>
</body>
</html>
```
```php
<?php
// 连接数据库
$conn = mysqli_connect("localhost", "root", "password", "test");
if (!$conn) {
die("连接失败: " . mysqli_connect_error());
}
// 获取查询关键字
$name = $_GET["name"];
// 查询学生信息
$sql = "SELECT * FROM t_student WHERE stuname LIKE '%$name%'";
$result = mysqli_query($conn, $sql);
// 输出学生信息
if (mysqli_num_rows($result) > 0) {
echo "<table border='1'>";
echo "<tr><th>学号</th><th>姓名</th><th>性别</th></tr>";
while($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row["stuno"] . "</td>";
echo "<td>" . $row["stuname"] . "</td>";
echo "<td>" . $row["stusex"] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "暂无符合条件的学生信息";
}
// 关闭数据库连接
mysqli_close($conn);
?>
```
阅读全文
相关推荐
















