1. 程式人生 > 實用技巧 >結對程式設計3——黃金點小遊戲實現區域網聯機

結對程式設計3——黃金點小遊戲實現區域網聯機

今日內容

  • 內容補充

  • 迴圈語句

  • 字串格式化

  • 運算子

  • 編碼

  • 單位

內容補充

條件語句補充

  • if條件語句的巢狀
'''需求列印10086服務選項
---------歡迎致電10086---------
1、套餐服務
2、投訴建議
3、業務辦理
4、人工服務
讓使用者選擇服務型別,並在業務辦理板塊加入流量業務及寬頻業務讓使用者選擇業務
'''
#!/usr/bin/env python
# -*- coding:utf-8 -*-
print('''						
---------歡迎致電10086---------
1、套餐服務
2、投訴建議
3、業務辦理
4、人工服務
''')
template = input('請輸入要辦理的業務:')		#等待使用者輸入辦理業務
template_int = int(template)
if template_int == 1:					   #判斷輸入業務
    print('套餐服務')
elif template_int == 2:
    print('投訴建議')
elif template_int == 3:					   #巢狀if重新判斷使用者輸入服務型別
    print('''
---------業務辦理 - --------
1、流量業務
2、寬頻業務 
    ''' )
    index = input('請輸入要辦理的服務:')
    index_int = int(index)
    if index == 1:
        print('流量業務')
    else:
        print('寬頻業務')
else:
    print('人工服務')

while迴圈語句

  • 基本while迴圈
#需求:死迴圈列印'我是帥哥'
while True:				#迴圈後跟條件,最後都會轉化為布林型別進行判斷
    print('我是帥哥')	 #執行結束,重新判斷條件是否滿足
  • while迴圈的巢狀
#需求:迴圈列印一次我是帥哥,再列印兩次才怪
score = 1
while True:
    print('我是帥哥')
    while score < 3:
        print('才怪')
        score += 1
    break
  • break 終止當前迴圈
#需求:終止死迴圈我是帥哥
while True:
    print('我是帥哥')
    break
  • contunue 結束本次迴圈開始下次迴圈
#需求:列印1 2 3 4 5 6 8 9 10
count = 1
while count <= 10:
    if count == 7:
        count += 1
        continue
    print(count)
    count += 1
  • while...else
#格式
while 條件:
    程式碼塊		  #當代碼塊中break跳出迴圈時不執行else中的程式碼塊
else:			#while條件不滿足時才會執行
    程式碼塊

字串格式化

  • 佔位符
    • %s
    • %d
  • 含有佔位符的字串想輸出%時要多加一個% 即%%
  • 字串格式化時要在變數最後加一個 ','
#需求讓使用者輸入個人資訊並列印個人資訊
name = input('請輸入姓名:')			#使用者輸入個人資訊
age = input('請輸入年齡:')
gender = input('請輸入性別:')
index = input('請輸入手機電量:')
age = int(age)
index = int(age)
template = """						#字串格式化
-----個人資訊-----
name  :%s
age   :%d
gender:%s
index :%d%%
-----------------
"""
print(template %(name, age, gender, index, )) #字串格式化在最後要加一個','

運算子

  • 算術運算子 : + - * / %取餘 **次方 //取整
  • 比較運算子: == > < >= <= !=不等於 ><不等於
  • 賦值運算子: = += -= *= /= %= **= //=
  • 成員運算子: in not in
  • 邏輯運算子: not and or
  • 面試題
value = not 1 or 9 and 0 	
print(value)			#0

  • 邏輯運算中優先順序 () > 算數運算 > 比較運算 > not > and > or

編碼

  • ascii
  • unicode
    • ecs2 用16位表示
    • ecs4 用32為表示
  • utf-8 中文用三個位元組表示,雖然gbk看似節省空間但utf-8國際通用所以推薦用utf-8編碼
  • gbk 中文用兩個位元組表示
  • gb2312

單位進位制

8位 == 1byte

1024byte == 1KB

1024KB == 1MB

1024MB == 1GB

1024GB == 1TB