python中,首先创建一个字典,键分别为姓名,学号,座号,并给出对应的值,由用户输入键,并输出对应的值,若键不存在,则输出“您输入的键不存在!要求以自己的姓名,学号,座号创建字典
时间: 2024-10-10 16:12:23 浏览: 50
在Python中,你可以通过下面的步骤来实现这个功能:
1. 首先,创建一个空字典,用于存储个人信息。例如:
```python
personal_info = {
"姓名": "",
"学号": "",
"座号": ""
}
```
2. 然后,提示用户输入姓名、学号和座号,并将这些值作为键对应到字典中:
```python
name = input("请输入您的姓名:")
student_id = input("请输入您的学号:")
seat_number = input("请输入您的座号:")
personal_info["姓名"] = name
personal_info["学号"] = student_id
personal_info["座号"] = seat_number
```
3. 接下来,编写一个函数来查询并显示信息:
```python
def show_info(key):
if key in personal_info:
print(f"{key}: {personal_info[key]}")
else:
print("您输入的键不存在!")
# 用户查询信息
query_key = input("请输入你想查找的信息的键(姓名、学号、座号):")
show_info(query_key)
```
4. 当用户输入一个键时,程序会检查这个键是否存在于`personal_info`字典中,如果存在就打印对应的值,否则提示键不存在。
相关问题
输入10个同学的座号,2门课成绩,按总分由高到低排序,并输出。
为了实现这个功能,我们可以假设每个同学都有两个分数,分别对应两门课程。这里是一个简单的Python示例,我们首先创建一个学生类,然后输入数据,计算总分并排序:
```python
class Student:
def __init__(self, id, score1, score2):
self.id = id
self.score1 = score1
self.score2 = score2
def main():
students = []
for _ in range(10):
id = int(input("请输入第{}位同学的座号:".format(_+1)))
score1 = float(input("请输入该同学第一门课的成绩:"))
score2 = float(input("请输入该同学第二门课的成绩:"))
student = Student(id, score1, score2)
students.append(student)
# 按照总分从高到低排序
students.sort(key=lambda x: x.score1 + x.score2, reverse=True)
print("按照总分从高到低排序结果:")
for i, student in enumerate(students, start=1):
print(f"第{i}名:座号{student.id}, 总分:{student.score1 + student.score2}")
if __name__ == "__main__":
main()
```
运行此程序,用户会依次输入每位学生的座号和两门课的成绩,最后将得到按总分排名的结果。
uni-app编写一个含有姓名,班级,学号,座号,性别,爱好,年龄,并有提交功能的个人信息表
好的,你可以按照以下步骤来实现:
1. 创建一个uni-app项目,选择合适的模板。
2. 在pages文件夹下创建一个新的页面,命名为“personalInfo”。
3. 在personalInfo页面中,使用uni-ui或者iview-weapp等UI组件库来实现表单的布局和样式。
4. 添加表单控件,包括输入框、单选框、多选框等,用于输入个人信息。
5. 在表单中添加一个提交按钮,点击后将表单数据提交到后台服务器。
6. 在uni-app的manifest.json配置文件中添加网络权限,确保应用可以访问网络。
7. 编写JS代码,将表单数据发送到后台服务器,并获取服务器返回的结果。
8. 根据服务器返回的结果,在页面中显示提交成功或提交失败的消息。
下面是一个简单的代码示例,供你参考:
```
<template>
<view class="container">
<form>
<view class="form-item">
<view class="form-label">姓名</view>
<input class="form-input" type="text" v-model="name" />
</view>
<view class="form-item">
<view class="form-label">班级</view>
<input class="form-input" type="text" v-model="class" />
</view>
<view class="form-item">
<view class="form-label">学号</view>
<input class="form-input" type="text" v-model="studentId" />
</view>
<view class="form-item">
<view class="form-label">座号</view>
<input class="form-input" type="text" v-model="seat" />
</view>
<view class="form-item">
<view class="form-label">性别</view>
<radio-group v-model="gender">
<radio value="male">男</radio>
<radio value="female">女</radio>
</radio-group>
</view>
<view class="form-item">
<view class="form-label">爱好</view>
<checkbox-group v-model="hobbies">
<checkbox value="reading">读书</checkbox>
<checkbox value="music">听音乐</checkbox>
<checkbox value="sports">运动</checkbox>
</checkbox-group>
</view>
<view class="form-item">
<view class="form-label">年龄</view>
<input class="form-input" type="text" v-model="age" />
</view>
<button class="submit-btn" @click="submit">提交</button>
</form>
</view>
</template>
<script>
export default {
data() {
return {
name: '',
class: '',
studentId: '',
seat: '',
gender: '',
hobbies: [],
age: '',
}
},
methods: {
submit() {
// 将表单数据发送到后台服务器
uni.request({
url: 'http://your-server-url.com/submit',
method: 'POST',
data: {
name: this.name,
class: this.class,
studentId: this.studentId,
seat: this.seat,
gender: this.gender,
hobbies: this.hobbies,
age: this.age,
},
success: (res) => {
// 根据服务器返回的结果,在页面中显示提交成功或提交失败的消息
if (res.statusCode === 200 && res.data.success) {
uni.showToast({
title: '提交成功',
icon: 'success',
})
} else {
uni.showToast({
title: '提交失败',
icon: 'none',
})
}
},
fail: () => {
uni.showToast({
title: '网络错误',
icon: 'none',
})
},
})
},
},
}
</script>
<style>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
form {
display: flex;
flex-direction: column;
align-items: center;
}
.form-item {
display: flex;
align-items: center;
margin-bottom: 20rpx;
}
.form-label {
width: 120rpx;
font-size: 28rpx;
}
.form-input {
flex: 1;
height: 56rpx;
border: 1rpx solid #999;
border-radius: 5rpx;
margin-left: 20rpx;
padding: 0 20rpx;
font-size: 28rpx;
}
.submit-btn {
width: 200rpx;
height: 80rpx;
background-color: #007aff;
border-radius: 40rpx;
color: #fff;
font-size: 28rpx;
margin-top: 40rpx;
}
</style>
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)