pythonl練習
阿新 • • 發佈:2017-09-09
env true for循環 print 程序員 bin 購物 數學運算 fde
練習:用戶輸入姓名、年齡、工作、愛好 ,然後打印成以下格式 ------------ info of Egon ----------- Name : Egon Age : 22 Sex : male Job : Teacher ------------- end -----------------
name=input(‘請輸入用戶名: ‘)
age=input(‘請輸入年齡: ‘)
sex=input(‘請輸入性別: ‘)
aihao=input(‘請輸入愛好: ‘)
a=‘‘‘
|-------楊天生個人信息--------|
| 名字: %s |
| 年齡: %s |
| 性別: %s |
| 愛好: %s |
|-----------------------------|
‘‘‘%(name,age,sex,aihao)
print(a)
運行的結果
-----------------------
------------------------------列表
a = ‘‘‘
aaaaaaa
aaaaaaa
bbbbbbbbbb
bbbbbbbbb
‘‘‘
print(a)
name = ‘aaa‘
aaa=‘17‘
name+aaa
a=[[‘yts‘,20], [‘aihao‘]]
print(a[1])
--------------------------------------字典
aaa=[
{‘name‘:‘yts‘,‘nianling‘:[‘20‘,‘22‘],‘aihao‘:[‘籃球‘,‘旅遊‘]},
{‘name‘: ‘wjy‘, ‘nianling‘:18, ‘aihao‘: [‘看書‘,‘旅遊‘]},
{‘name‘: ‘yct‘, ‘nianling‘:20, ‘aihao‘: [‘遊戲‘,‘臺球‘]},
]
print(aaa[0][‘nianling‘][0]) #取第1列的年齡第1個
print(aaa[1][‘aihao‘][1]) #取第二列的愛好第二個 #在python中數字排序都是從0開始的 所以0代表第一位
print(aaa[1][‘name‘]) #取第二列的 姓名
b={
‘姓名‘:‘楊天生‘,
‘愛好‘:[‘籃球‘,‘臺球‘],
‘公司信息‘:{
‘公司名稱‘:‘淘寶‘,
‘類型‘:‘購物‘,
‘號碼‘:[‘110‘,‘120‘],
}
}
print(b[‘name‘])
print(b[‘公司信息‘][‘公司名稱‘]) #取公司名稱
print(b[‘公司信息‘][‘號碼‘][1]) #取公司第幾個號碼
print(b[‘愛好‘][1]) 取愛好第幾個
------------------------------------------------------
------------------------------------布爾
true=真 false=假
a=3
b=5
print(a > b) #-----False 假
print(a < b) #----True 真
if a > b
print(a is bigger than b )
else
print(a is smaller than b )
1.可變類型:在id不變的情況下,value可以變,則稱為可變類型,如列表,字典
2. 不可變類型:value一旦改變,id也改變,則稱為不可變類型(id變,意味著創建了新的內存空間)
-------------------------------------------------------
-------------------------格式化輸出
print(‘姓名:%s, 年齡:%d ‘ %(‘yts‘,11)), # --------%s字符串占位符:可以接收字符串,也可接收數字
----------------%d數字占位符:只能接收數字
@age=input(‘your age: ‘)
print(‘My name is %s,my age is %s‘ %(name,age))
print(‘My name is %s,my age is %d‘ %(name,age)) #----用戶輸入18,會存成字符串18,無法傳給%d #age為字符串類型,無法傳給%d,所以會報錯
#---------------流程控制if....else
既然我們編程的目的是為了控制計算機能夠像人腦一樣工作,那麽人腦能做什麽,就需要程序中有相應的機制去模擬。人腦無非是數學運算和邏輯運算,對於數學運算在上一節我們已經說過了。對於邏輯運算,即人根據外部條件的變化而做出不同的反映,比如
1 如果:女人的年齡>30歲,那麽:叫阿姨
age_of_girl=31 if age_of_girl > 30: print(‘阿姨好‘)
2 如果:女人的年齡>30歲,那麽:叫阿姨,否則:叫小姐
age_of_girl=18 if age_of_girl > 30: print(‘阿姨好‘) else: print(‘小姐好‘)
3 如果:女人的年齡>=18並且<22歲並且身高>170並且體重<100並且是漂亮的,那麽:表白,否則:叫阿姨
age_of_girl=18 height=171 weight=99 is_pretty=True if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True: print(‘表白...‘)else: print(‘阿姨好‘)
如果:成績>=90,那麽:優秀
如果成績>=80且<90,那麽:良好
如果成績>=70且<80,那麽:普通
其他情況:很差
a=input(‘請輸入你的成績: ‘)
a=int(a)
if a >= 90:
print(‘優秀‘)
elif a >= 80:
print(‘良好‘)
elif a >= 70:
print(‘一般‘)
else:
print(‘不及格‘)
if 條件1: 縮進的代碼塊 elif 條件2: 縮進的代碼塊 elif 條件3: 縮進的代碼塊 ...... else: 縮進的代碼塊
-----------
簡單的輸入用戶名
#!/usr/bin/env python
# -*- coding: utf-8 -*-
name=input(‘請輸入用戶名: ‘)
password=input(‘請輸入密碼: ‘)
if name == ‘yts‘ and password == ‘123‘:
print(‘輸入正確‘)
else:
print(‘輸入的用戶名和密碼都不正確baye‘)
--------------------------
根據用戶輸入內容輸出其權限
‘‘‘
yts ------>CEO
test ------>CTO
tom ,nick------->CKO
其他 ------->員工
‘‘‘
name=input(‘請輸入用戶名字: ‘)
if name == ‘yts‘:
print(‘CEO‘)
elif name == ‘test‘:
print(‘CTO‘)
elif name == ‘tom‘ or name == ‘nick‘:
print(‘CKO‘)
else:
print(‘員工‘)
-----------------------
# 如果:今天是Monday,那麽:上班 # 如果:今天是Tuesday,那麽:上班 # 如果:今天是Wednesday,那麽:上班 # 如果:今天是Thursday,那麽:上班 # 如果:今天是Friday,那麽:上班 # 如果:今天是Saturday,那麽:出去浪 # 如果:今天是Sunday,那麽:出去浪 #方式一: today=input(‘>>: ‘) if today == ‘Monday‘: print(‘上班‘) elif today == ‘Tuesday‘: print(‘上班‘) elif today == ‘Wednesday‘: print(‘上班‘) elif today == ‘Thursday‘: print(‘上班‘) elif today == ‘Friday‘: print(‘上班‘) elif today == ‘Saturday‘: print(‘出去浪‘) elif today == ‘Sunday‘: print(‘出去浪‘) else: print(‘‘‘必須輸入其中一種: Monday Tuesday Wednesday Thursday Friday Saturday Sunday ‘‘‘) #方式二: today=input(‘>>: ‘) if today == ‘Saturday‘ or today == ‘Sunday‘: print(‘出去浪‘) elif today == ‘Monday‘ or today == ‘Tuesday‘ or today == ‘Wednesday‘ or today == ‘Thursday‘ or today == ‘Friday‘: print(‘上班‘) else: print(‘‘‘必須輸入其中一種: Monday Tuesday Wednesday Thursday Friday Saturday Sunday ‘‘‘) #方式三: today=input(‘>>: ‘) if today in [‘Saturday‘,‘Sunday‘]: print(‘出去浪‘) elif today in [‘Monday‘,‘Tuesday‘,‘Wednesday‘,‘Thursday‘,‘Friday‘]: print(‘上班‘) else: print(‘‘‘必須輸入其中一種: Monday Tuesday Wednesday Thursday Friday Saturday Sunday ‘‘‘)
-----------------------
流程控制之while循環
為何要用循環語句
#上節課我們已經學會用if .. else 來猜年齡的遊戲啦,但是只能猜一次就中的機率太小了,如果我想給玩家3次機會呢?就是程序啟動後,玩家最多可以試3次,這個怎麽弄呢?你總不會想著把代碼復制3次吧。。。。
nick=20
test = int (input(‘請輸入你要猜測的數字: ‘))
if test > nick:
print(‘太大了‘)
elif test < nick:
print(‘太小啦‘)
else:
print(‘答對了‘)
#即使是小白的你,也覺得的太low了是不是,以後要修改功能還得修改3次,因此記住,寫重復的代碼是程序員最不恥的行為。 那麽如何做到不用寫重復代碼又能讓程序重復一段代碼多次呢? 循環語句就派上用場啦
2 條件循環:while,語法如下
while 條件: # 循環體 # 如果條件為真,那麽循環體則執行,執行完畢後再次循環,重新判斷條件。。。 # 如果條件為假,那麽循環體不執行,循環終止
#打印0-10 count=0 while count <= 10: print(‘loop‘,count) count+=1 #打印0-10之間的偶數 count=0 while count <= 10: if count%2 == 0: print(‘loop‘,count) count+=1 #打印0-10之間的奇數 count=0 while count <= 10: if count%2 == 1: print(‘loop‘,count) count+=1----------------- 死循環
# import time
# a=0
# while True:
# print(‘count‘,a)
# time.sleep(1)
# a+=1
------------------
#練習,要求如下: 1 循環驗證用戶輸入的用戶名與密碼 2 認證通過後,運行用戶重復執行命令 3 當用戶輸入命令為quit時,則退出整個程序
#實現一: name=‘egon‘ password=‘123‘ while True: inp_name=input(‘用戶名: ‘) inp_pwd=input(‘密碼: ‘) if inp_name == name and inp_pwd == password: while True: cmd=input(‘>>: ‘) if not cmd:continue if cmd == ‘quit‘: break print(‘run <%s>‘ %cmd) else: print(‘用戶名或密碼錯誤‘) continue break #實現二:使用tag name=‘egon‘ password=‘123‘ tag=True while tag: inp_name=input(‘用戶名: ‘) inp_pwd=input(‘密碼: ‘) if inp_name == name and inp_pwd == password: while tag: cmd=input(‘>>: ‘) if not cmd:continue if cmd == ‘quit‘: tag=False continue print(‘run <%s>‘ %cmd) else: print(‘用戶名或密碼錯誤‘)
4 break與continue
#break用於退出本層循環 while True: print "123" break print "456" #continue用於退出本次循環,繼續下一次循環 while True: print "123" continue print "456"
-------------------------
5 while+else
--------------------十四 流程控制之for循環
1 叠代式循環:for,語法如下
for i in range(10):
縮進的代碼塊
2 break與continue(同上)
3 循環嵌套
for i in range(1,100):
for j in range(1,i+1):
print(‘%s*%s=%s‘ %(i,j,i*j),end=‘ ‘)
print()
----------------
基礎需求:
- 讓用戶輸入用戶名密碼
- 認證成功後顯示歡迎信息
- 輸錯三次後退出程序
dic={ ‘egon1‘:{‘password‘:‘123‘,‘count‘:0}, ‘egon2‘:{‘password‘:‘123‘,‘count‘:0}, ‘egon3‘:{‘password‘:‘123‘,‘count‘:0}, } while True: name=input(‘username>>: ‘) if not name in dic: print(‘用戶不存在‘) continue if dic[name][‘count‘] > 2: print(‘嘗試次數過多,鎖定‘) continue password=input(‘password>>: ‘) if password == dic[name][‘password‘]: print(‘登錄成功‘) break else: print(‘用戶名或密碼錯誤‘) dic[name][‘count‘]+=1
#與其它語言else 一般只與if 搭配不同,在Python 中還有個while ...else 語句,while 後面的else 作用是指,當while 循環正常執行完,中間沒有被break 中止的話,就會執行else後面的語句 count = 0 while count <= 5 : count += 1 print("Loop",count) else: print("循環正常執行完啦") print("-----out of while loop ------") 輸出 Loop 1 Loop 2 Loop 3 Loop 4 Loop 5 Loop 6 循環正常執行完啦 -----out of while loop ------ #如果執行過程中被break啦,就不會執行else的語句啦 count = 0 while count <= 5 : count += 1 if count == 3:break print("Loop",count) else: print("循環正常執行完啦") print("-----out of while loop ------") 輸出 Loop 1 Loop 2 -----out of while loop ------
#在表白的基礎上繼續: #如果表白成功,那麽:在一起 #否則:打印。。。 age_of_girl=18 height=171 weight=99 is_pretty=True success=False if age_of_girl >= 18 and age_of_girl < 22 and height > 170 and weight < 100 and is_pretty == True: if success: print(‘表白成功,在一起‘) else: print(‘什麽愛情不愛情的,愛nmlgb的愛情,愛nmlg啊...‘) else: print(‘阿姨好‘)
pythonl練習