python之路:第一周笔记

2018-07-04 02:16:24来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

  1 ###python中默认所有的输入为string类型
  2 ###python中代码实行强制缩进,缩进错误代码:IndenttationError
  3 --------------------------------------------------------------------------------
  4 第14章
  5        if else 流程判断
  6           if 变量 = 变量 and 变量 = 变量 :
  7              print"xxxx").format(变量)
  8           else:
  9              print"xxxx").format(变量)
 10 
 11 --------------------------------------------------------------------------------
 12 1.continue 用法:跳出本次循环,继续到下一次循环。
 13 if i < 3:
 14     print("loop",i)
 15 else:
 16     continue #当i>=3 则执行此处,跳出本次循环,回到for 执行下一次循环
 17 print("hehe")
 18 
 19 --------------------------------------------------------------------------------
 20  2.循环套循环:大循环套小循环,break用法:break结束当前循环。
 21 for i in range(0,10):
 22     print("-----",i)
 23     for j in range(10):
 24         print(j)
 25         if j>5:
 26             break
 27 
 28 
 29 --------------------------------------------------------------------------------
 30 3.'''xxx '''' 三个单引号的作用:
 31 1.注释   2. 打印多行
 32 
      
 33 
 34 --------------------------------------------------------------------------------
 35 4.用户输入
 36 变量 = input(“xxx‘’)
 37 print(变量)
 38 #Author:andxu
 39 username = input("please enter username:")
 40 password = input("please enter password:")
 41 age = input("please enter age:")
 42 sex = input("please enter sex:")
 43 print(username,password)
 44 
 45 #outcome
 46 please enter username:151
 47 please enter password:1651
 48 please enter age:5616
 49 please enter sex:15
 50 
 51 --------------------------------------------------------------------------------
 52 5.字符串拼接 
 53            第一种:'''  '''  三引号 + 拼接
 54            第二种:
 55 
       56
 57 
 58 --------------------------------------------------------------------------------
 59 第一种code:
 60 #Author:andxu
 61 username = input("please enter username:")
 62 password = input("please enter password:")
 63 age = input("please enter age:")
 64 sex = input("please enter sex:")
 65 #print(username,password)
 66 info = '''
 67 ------ info of ''' + username + ''' ------
 68 Name:''' + username + '''
 69 Password:''' +password + '''
 70 age:''' + age + '''
 71 sex: ''' + sex + '''
 72 '''
 73 print(info)
 74 
 75 --------------------------------------------------------------------------------
 76 第二种code: (s代表string,一个字符类型。d代表只能接受数字,帮助检测验证数据类型。f代表浮点小数。)
 77 #Author:andxu
 78 username = input("please enter username:")
 79 password = input("please enter password:")
 80 age = input("please enter age:")
 81 sex = input("please enter sex:")
 82 #print(username,password)
 83 info = '''
 84 ------ info of %s ------
 85 Name:%s
 86 Password:%s
 87 age:%s
 88 sex: %s'
 89 ''' %(username,username,password,age,sex)
 90 print(info)
 91 
 92 --------------------------------------------------------------------------------
 93 第三种code:用format函数
 94 #Author:andxu
 95 username = input("please enter username:")
 96 password = input("please enter password:")
 97 age = input("please enter age:")
 98 sex = input("please enter sex:")
 99 #print(username,password)
100 info1 = '''
101 ------ info of {_username} ------
102 Name:{_username}
103 Password:{_password}
104 age:{_age}
105 sex: {_sex}
106 ''' .format(_username = username,
107             _password = password,
108             _age = age,
109             _sex = sex)
110 print(info1)
111 
112 #Author:andxu
113 username = input("please enter username:")
114 password = input("please enter password:")
115 age = input("please enter age:")
116 sex = input("please enter sex:")
117 #print(username,password)
118 info2 = '''
119 ------ info of {0} ------
120 Name:{0}
121 Password:{1}
122 age:{2}
123 sex: {3}
124 ''' .format(username,username,password,age,sex)
125 print(info2)
126 
127 --------------------------------------------------------------------------------
128 6.打印一个数据的变量类型:
129 print(type(age))
130 4.转换数据类型
131 age = int(input("age:"))
132 print(type(age),type(str(age)))
133 
134 --------------------------------------------------------------------------------
135 7.明文变密文
136 #Author:andxu
137 import getpass #导入引用原有模块变量
138 username = input("username:")
139 password = getpass.getpass("password:")
140 
141 print(username,password)
142 此操作只能在命令提示符cmd命令窗口内
     
143 
144 --------------------------------------------------------------------------------
145 8.while 循环
146 count = 0
147 while True:
148     print("count:")
149     count = count +1   #count +=
150 
151 --------------------------------------------------------------------------------
152 9.#python小游戏:
153 猜年龄
154 #Author:Andxu
155 
156 age_of_xusj = 23
157 guess_age = int(input("guess age:"))
158 if guess_age == age_of_xusj:
159     print("yes,you got it!")
160 elif guess_age > age_of_xusj:
161     print("guess smaller!")
162 else:
163     print("guess bigger!")
164 
165 --------------------------------------------------------------------------------
166 循环猜年龄
167 #Author:Andxu
168 
169 age_of_xusj = 23
170 #count = 3
171 while True:
172     guess_age = int(input("guess age:"))
173     if guess_age == age_of_xusj:
174         print("yes,you got it!")
175     elif guess_age > age_of_xusj:
176         print("guess smaller!")
177     else:
178         print("guess bigger!")
179 
180 --------------------------------------------------------------------------------
181 循环猜年龄,猜对退出 break函数
182 #Author:Andxu
183 
184 age_of_xusj = 23
185 #count = 3
186 while True:
187     guess_age = int(input("guess age:"))
188     if guess_age == age_of_xusj:
189         print("yes,you got it!")
190         break
191     elif guess_age > age_of_xusj:
192         print("guess smaller!")
193     else:
194         print("guess bigger!")
195 
196 --------------------------------------------------------------------------------
197 循环猜年龄,限制次数退出。
198 #Author:Andxu 
199 
200 age_of_xusj = 23
201 count = 0  #计数器
202 while True:  #为真,当条件成立
203     if count ==3:
204         break  #破坏本次循环,直接退出!
205     guess_age = int(input("guess age:"))
206     if guess_age == age_of_xusj:
207         print("yes,you got it!")
208         break  #破坏本次循环,直接退出!
209     elif guess_age > age_of_xusj:
210         print("guess smaller!")
211     else:
212         print("guess bigger!")
213     count = count +1 #次数自增1
214 
215 优化:
216 #Author:Andxu
217 
218 age_of_xusj = 23
219 count = 0  #计数器
220 while  count <3:  #当count小于3 为真,执行以下循环
221     guess_age = int(input("guess age:"))
222     if guess_age == age_of_xusj:
223         print("yes,you got it!")
224         break  #破坏本次循环,直接退出!
225     elif guess_age > age_of_xusj:
226         print("guess smaller!")
227     else:
228         print("guess bigger!")
229     count +=1 #次数自增1
230 
231 --------------------------------------------------------------------------------
232 猜年龄输入3次错误提示:
233 #Author:Andxu
234 
235 age_of_xusj = 23
236 count = 0  #计数器
237 while  count <3:  #当count小于3 为真,执行以下循环
238     guess_age = int(input("guess age:"))
239     if guess_age == age_of_xusj:
240         print("yes,you got it!")
241         break  #破坏本次循环,直接退出!
242     elif guess_age > age_of_xusj:
243         print("guess smaller!")
244     else:
245         print("guess bigger!")
246     count +=1 #次数自增1
247 if count ==3: #当次数等于3执行以下print提示
248     print("you have tried too many times.. you fuck off")
249 
250 猜年龄:猜错三次询问继续否
251 #Author:Andxu
252 
253 age_of_xusj = 23
254 count = 0  #计数器
255 while  count <3:  #当count小于3 为真,执行以下循环
256     guess_age = int(input("guess age:"))
257     if guess_age == age_of_xusj:
258         print("yes,you got it!")
259         break  #破坏本次循环,直接退出!
260     elif guess_age > age_of_xusj:
261         print("guess smaller!")
262     else:
263         print("guess bigger!")
264     count +=1 #次数自增1
265     if count == 3:
266         countine_confirm = input("do you want to keep guessing ? : ")
267         if countine_confirm != 'n':
268             count = 0
269 
270 --------------------------------------------------------------------------------
271 10.for 循环
272 for i in  range(10): #range 相当于一个变量数据集(0,1,2,3,4,5,6,7,8,9)
273     print("loop",i)
274 
275 result:
276 F:\CXYY\python3.2\venv\Scripts\python.exe F:/CXYY/python3.2/for.py
277 loop 0
278 loop 1
279 loop 2
280 loop 3
281 loop 4
282 loop 5
283 loop 6
284 loop 7
285 loop 8
286 loop 9
287 
288 进程已结束,退出代码0
289 
290 for 循环 跳数打印:
291  # (0,10,2)表示从0到10,2表示步长,默认为1,即range(0,10)= range(0,10,1)
292 
293 for i in  range(0,10,2): #range 相当于变量数据集(0,1,2,3,4,5,6,7,8,9)
294     print("loop",i)
295 
296 result:
297 
298 for用于猜年龄游戏
299 #Author:Andxu
300 
301 age_of_xusj = 23
302 for i in range(3):  #当count小于3 为真,执行以下循环
303     guess_age = int(input("guess age:"))
304     if guess_age == age_of_xusj:
305         print("yes,you got it!")
306         break  #破坏本次循环,直接退出!
307     elif guess_age > age_of_xusj:
308         print("guess smaller!")
309     else:
310         print("guess bigger!")
311 else:
312     print("you have tried too many times.. you fuck off")
313 
314 --------------------------------------------------------------------------------
315 
316 
317                                                               date:20180703 xsj
318                                                                                                                               

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python-修行第一天

下一篇:使用pyinstaller编译python文件