学习总结:
变量,字符的由来,python2和python3的区别,控制语句:if,for,while,break,continue用法
学习示例:
if用法1:判断年龄
1 # -*- coding: utf-8 -*- 2 # Author:Yang Hang 3 4 age = 75 5 6 guess_age = input("请输入年龄:") 7 guess_age = int(guess_age) 8 9 if guess_age == age: 10 print("你猜对了!") 11 elif guese_age > age: 12 print("请往小的猜!") 13 else: 14 print("请往大的猜!")
if用法2:登陆示例
1 # -*- coding: utf-8 -*- 2 # Author:Yang Hang 3 4 _username = 'yanghang' 5 _passwd = '123456' 6 7 username = input("username:") 8 passwd = input("password:") 9 10 if username == _username and passwd == _passwd: 11 print("成功登陆!") 12 else: 13 print("用户名或密码有误,请重新输入!")
for用法1:输出1到10
1 # -*- coding: utf-8 -*- 2 # Author:Yang Hang 3 4 for i in range(10): 5 print("loop",i)
for用法2:猜年龄
1 # -*- coding: utf-8 -*- 2 # Author:Yang Hang 3 4 age = 75 5 count = 0 6 7 8 for i in range(3): 9 guess_age = int(input("请输入年龄:")) 10 if guess_age == age: 11 print("恭喜你猜对了!") 12 break 13 elif guess_age > age: 14 print("请往小的猜!") 15 else: 16 print("请往大的猜!") 17 else: 18 print("输入次数超过三次,结束")
while用法1:
1 # -*- coding: utf-8 -*- 2 # Author:Yang Hang 3 4 count = 0 5 while count < 100: 6 print("count:",count) 7 count = count +1
while用法2:
1 # -*- coding: utf-8 -*- 2 # Author:Yang Hang 3 4 age = 75 5 count = 0 6 ''' 7 #版本1: 8 while True: 9 if count == 3: 10 print("输入次数超过三次,结束") 11 break 12 guess_age = int(input("请输入年龄:")) 13 if guess_age == age: 14 print("恭喜你猜对了!") 15 break 16 elif guess_age > age: 17 print("请往小的猜!") 18 else: 19 print("请往大的猜!") 20 count += 1 21 ''' 22 #优化版本 23 while count <3: 24 guess_age = int(input("请输入年龄:")) 25 if guess_age == age: 26 print("恭喜你猜对了!") 27 break 28 elif guess_age > age: 29 print("请往小的猜!") 30 else: 31 print("请往大的猜!") 32 count +=1 33 if count == 3: 34 countine_confirm =input("你还想继续猜吗..?") 35 if countine_confirm != 'n': 36 count = 0 37 else: 38 print("输入次数超过三次,结束")