学习总结:
变量,字符的由来,python2和python3的区别,控制语句:if,for,while,break,continue用法
学习示例:
if用法1:判断年龄
# -*- coding: utf-8 -*- # Author:Yang Hang age = 75 guess_age = input("请输入年龄:") guess_age = int(guess_age) if guess_age == age: print("你猜对了!") elif guese_age > age: print("请往小的猜!") else: print("请往大的猜!")
if用法2:登陆示例
# -*- coding: utf-8 -*- # Author:Yang Hang _username = 'yanghang' _passwd = ' username = input("username:") passwd = input("password:") if username == _username and passwd == _passwd: print("成功登陆!") else: print("用户名或密码有误,请重新输入!")
for用法1:输出1到10
# -*- coding: utf-8 -*- # Author:Yang Hang for i in range(10): print("loop",i)
for用法2:猜年龄
# -*- coding: utf-8 -*- # Author:Yang Hang age = 75 count = 0 for i in range(3): guess_age = int(input("请输入年龄:")) if guess_age == age: print("恭喜你猜对了!") break elif guess_age > age: print("请往小的猜!") else: print("请往大的猜!") else: print("输入次数超过三次,结束")
while用法1:
# -*- coding: utf-8 -*- # Author:Yang Hang count = 0 while count < 100: print("count:",count) count = count +1
while用法2:
# -*- coding: utf-8 -*- # Author:Yang Hang age = 75 count = 0 ''' #版本1: while True: if count == 3: print("输入次数超过三次,结束") break guess_age = int(input("请输入年龄:")) if guess_age == age: print("恭喜你猜对了!") break elif guess_age > age: print("请往小的猜!") else: print("请往大的猜!") count += 1 ''' #优化版本 while count <3: guess_age = int(input("请输入年龄:")) if guess_age == age: print("恭喜你猜对了!") break elif guess_age > age: print("请往小的猜!") else: print("请往大的猜!") count +=1 if count == 3: countine_confirm =input("你还想继续猜吗..?") if countine_confirm != 'n': count = 0 else: print("输入次数超过三次,结束")