導航:首頁 > 編程語言 > python生成數據字典

python生成數據字典

發布時間:2023-08-06 19:47:36

python作業:讓用戶輸入鍵和值生成字典,然後通過值查找對應的鍵

#以輸入3個值和鍵為例
dic={}
foriinrange(0,3):
key=input("輸入建:")
val=input("輸入值:")
ifkeynotindic.keys():
dic[key]=val

print("輸入完成。")
key=input("輸入要查找的鍵:")
ifkeyindic.keys():
print(key,'的值為:',dic[key])
else:
print('字典中沒有這個鍵')

效果如下:

⑵ 如何用python的字典和列表來實現學生成績管理

下面是一個使用 Python 的字典和列表來實現學生成績管理的簡單例子。此例子實現了所有要求,但沒有使用定義學生結構體類型和數組:


# 定義學生數據字典

students = []

# 定義輸入函數

def input_student():

while True:

student = {}

student['id'] = input('學號: ')

student['class'] = input('班級: ')

student['name'] = input('姓名: ')

student['scores'] = []

for i in range(3):

score = input('第%d門課程成績: ' % (i + 1))

student['scores'].append(score)

students.append(student)

if input('是否穗讓瞎繼續輸入(y/n): ') != 'y':

break

# 定義求平均分函數

def average_score():

for student in students:

total = 0

for score in student['scores']:

total += score

student['average'] = total / len(student['scores'])

# 定義求最高平均分函數

def max_average():

max_student = None

max_average = 0

for student in students:

if student['average'] > max_average:

max_student = student

max_average = student['average']

return max_student

# 調用輸入函數

input_student()

# 調用求平均分函數

average_score()

# 輸出每個學生的3門課程平均分

for student in students:

print('學號: %s, 班級: %s, 姓名: %s, 平均分: %.2f' % (student['id'], student['class'], student['name'], student['average']))

# 調用求最高平均分函數

max_student = max_average()

# 輸出最高平均分的學生信息

if max_student:

print(' 平均分猜空最高的學生: 學號: %s, 班級: %s, 姓名: %s, 3門課程成績: %s, 平均分: %.2f' % (max_student['id'], max_student['class'], max_student['name'], max_student['scores'], max_student['average']))


在上面的例子中,我們定義了一個學生數據字典,用於存儲學生信息。然後定義了三個函數,分別用於輸入學生信息滑爛、求每個學生3門課程的平均分和求平均分最高的學生。最後,在主函數中調用這三個函數,並輸出結果。

閱讀全文

與python生成數據字典相關的資料

熱點內容
postgresqllinux命令 瀏覽:510
編譯原理翻譯文法的功能 瀏覽:441
51單片機LCD電路 瀏覽:890
我的世界如何玩寶可夢伺服器 瀏覽:258
天天象棋app怎麼找不到了 瀏覽:658
如何格式化內存卡上加密的照片 瀏覽:32
上汽大眾app哪裡上傳發票 瀏覽:115
手機電池加密屏幕加密 瀏覽:385
基於51系列單片機的智能家居 瀏覽:584
看新聞看哪個app 瀏覽:273
android編輯對話框 瀏覽:308
出租天津伺服器雲空間 瀏覽:792
cmd命令結束進程 瀏覽:445
程序員逗比圖片大全 瀏覽:515
免編程游戲引擎 瀏覽:375
微信應用鎖加密 瀏覽:460
數字英雄對程序員的影響 瀏覽:761
程序員培訓學校排名 瀏覽:586
oppo手機如何解開微信的加密 瀏覽:46
學校論文答辯源碼 瀏覽:75