学习总结2019.3.21

一、if判断

'''
if的使用:
语法一:
if 条件: # 条件成立时执行以下子代码块
code one
code two

语法二:
if 条件: # 条件成立时执行以下子代码块
code one
code two
else: # 条件不成立时执行以下子代码块
code one
code two

语法三:
if 条件1:
if 条件2:
code one
code two

语法四:
if 条件1:
code one
code two
elif 条件2:
code one
code two
......
else:
code one
code two
'''

# 语法一示例:
sex = 'female'
age = 18
is_beautiful = True

if sex == 'female' and age > 16 and age < 20 and is_beautiful:
print('What a such beauty!.Can we...')

# 语法二示例:
sex = 'female'
age = 38
is_beautiful = True

if sex == 'female' and age > 16 and age < 20 and is_beautiful:
print("What a such beauty!.Can we...")
else:
print("No,we can't")

# 语法三示例:
sex = 'female'
age = 18
is_beautiful = True
is_successful = True
# 通过\可以转义回车键,使一条长代码用两行来表达
if sex == 'female' and age > 16 and \
age < 20 and is_beautiful:
print("What a such beauty!.Can we...")
if is_successful:
print('heiheihei')
else:
print('爱情是什么玩意')
else:
print("No,we can't")
 

二、while循环

'''
while的使用
一、while的语法:
while 条件:
code one
code two

二、结束while循环的两种方式:
1、条件改为False
在条件改为False时不会立即结束循环,而是要等到下次循环判断条件时才生效
2、while + break
break一定要放在循环体内,一旦循环执行到break就会立即结束本层循环

三、while + continue:
结束本次循环,直接进入下一次循环

四、while嵌套使用:
while 条件1:
while 条件2:
code one
code two

五、while + else:
while 条件:
code one
code two
else:
在循环结束后,并且在循环未被break打断过的情况下,才会执行else的代码
'''

# 示例一:
while True:
name = input('please input your name:')
pwd = input('please input your password:')

if name == 'egon' and pwd == '123':
print('login successfully')
while True:
print('''
0 退出
1 取款
2 转账
3 查询
''')
choice = int(input('请输入您要执行的操作:'))
if choice == 0:
break
elif choice == 1:
print('这是取款,但是没钱,谢谢!')
elif choice == 2:
print('这是转账,麻烦转给我,谢谢!')
elif choice == 3:
print('查询,显示为零')
else:
print('您输入的指令有误,请重新输入')
break
else:
print('username or password error')

# 示例二:
tag = True
while tag:
username = input('please input your username:')
pwd = input('please input your password:')

if username == 'egon' and pwd == '123':
print('login successfully')
while tag:
print('''
0 退出
1 取款
2 转账
3 查询
''')
choice = input('请输入您要执行的操作:')
if choice == '0':
tag = False
elif choice == '1':
print('这是取款,但是没钱,谢谢!')
elif choice == '2':
print('这是转账,麻烦转给我,谢谢!')
elif choice == '3':
print('查询,显示为零')
else:
print('您输入的指令有误。请重新输入')
else:
print('username or password error')

# 示例三:
count = 1
while count < 6:
if count == 4:
count += 1
continue
print(count)
count += 1
 

三、for循环

'''
for循环的语法如下:
for i in range(10):
code one

for循环有跟while循环一样的使用方法,包括:
for + break
for + continue
for循环嵌套
for + else

for循环的强大之处在于循环取值
l = ['a','b','c','d','e']
for i in l:
print(i)
'''

# 九九乘法口诀表
for i in range(1,10):
for j in range(1,i+1):
print('%d*%d=%d'%(j,i,j*i),end = " ")
print('\n')

猜你喜欢

转载自www.cnblogs.com/penghengshan/p/10573519.html