Python数据处理相关小例编程
生活随笔
收集整理的這篇文章主要介紹了
Python数据处理相关小例编程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有5名某界大佬xiaoyun、xiaohong、xiaoteng、xiaoyi和xiaoyang,其QQ號分別是88888、5555555、11111、1234321和1212121,用字典將這些數據組織起來。編程實現以下兩個功能:
(1)用戶輸入某一個大佬的姓名后可以輸出其QQ號,如果輸入的姓名不在字典中則返回提示信息并允許再次輸入;
(2)尋找所有有QQ靚號(5位數或小于5位數)的大佬,輸出所有姓名。
其中Python 2中提示輸入和輸出結果的兩句提示語請使用如下形式:
name = raw_input("Please input the name:")
print ?"Who has the nice QQ number?"
其中Python 3中提示輸入和輸出結果的兩句提示語請使用如下形式:
name = input("Please input the name:")
print("Who has the nice QQ number?")
>>> adict = {'xiaoyun':88888, 'xiaohong':5555555, 'xiaoteng':11111, 'xiaoyi':1234321, 'xiaoyang':1212121} >>> def qq():name = input('Please input the name:')if name in adict.keys():print(adict[name])else:print( 'The name does not exist.')a = input('Try again:y or n?')if a == 'y':qq()else:return 'Bey!'>>> qq() Please input the name:q The name does not exist. Try again:y or n?y Please input the name:xiaoyun 88888 >>> qq() Please input the name:xiaoyun 88888 >>> qq() Please input the name:q The name does not exist. Try again:y or n?n 'Bey!' >>> def nm(): <span style="white-space:pre"> </span>print('Who has the nice QQ number?') <span style="white-space:pre"> </span>for i in adict.keys(): #!!! <span style="white-space:pre"> </span>if len(str(adict[i])) <= 5: #整數沒有長度,要轉化成字符串 <span style="white-space:pre"> </span>print(i)<span style="white-space:pre"> </span> >>> nm() Who has the nice QQ number? xiaoyun xiaoteng
總結
以上是生活随笔為你收集整理的Python数据处理相关小例编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于九宫格的一个算法,求大神助攻
- 下一篇: 用python玩转数据测试与作业_用py