1 条件语句
if 条件:
...
else:
...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Chuncheng.Fan <xmzncc@gmail.com>
username = input("请输入用户名:")
password = input("请输入密码:")
if username == 'fcc' and password == '123':
print("欢迎登陆!")
else:
print("用户名或密码错误!")
if 条件:
...
elif 条件:
...
else:
...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Chuncheng.Fan <xmzncc@gmail.com>
sex = input("请输入你的性别:")
if sex == "男":
print("傻x,自己性别都忘了!")
elif sex == "女":
print("...你在想想,你忘了你已经做了手术了吗...")
else:
print("人妖.......")
2 循环语句
while 条件:
continue # 开始下一次循环
break # 跳出所有循环
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Chuncheng.Fan <xmzncc@gmail.com>
i = 0
while i < 3:
username = input("请输入用户名:")
password = input("请输入密码:")
if username == 'fcc' and password == '123':
print("欢迎登陆!")
break
else:
print('用户名或密码错误')
i += 1
3 练习
- 使用while循环输入 1 2 3 4 5 6 8 9 10
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Chuncheng.Fan <xmzncc@gmail.com>
i = 1
while True:
if i == 7:
i += 1
continue
print(i)
i += 1
if i == 11:
break
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Chuncheng.Fan <xmzncc@gmail.com>
value = 0
i = 1
while i < 101:
value = value + i
i += 1
print(value)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Chuncheng.Fan <xmzncc@gmail.com>
i = 1
while i < 101:
if i % 2 == 1:
print(i)
i += 1
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Chuncheng.Fan <xmzncc@gmail.com>
i = 1
while i < 101:
if i % 2 == 0:
print(i)
i += 1
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Chuncheng.Fan <xmzncc@gmail.com>
value = 0
i = 1
while i < 100:
if i % 2 == 1:
value = value + i
i += 1
elif i % 2 == 0:
value = value - i
i +=1
print(value)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author:Chuncheng.Fan <xmzncc@gmail.com>
i = 0
while i < 3:
username = input("请输入用户名:")
password = input("请输入密码:")
if username == 'fcc' and password == '123':
print("欢迎登陆!")
break
else:
print("用户名或密码错误")
i += 1