一、分支结构
1. if else
number = eval(input('please tell me a number:'))
print('恭喜中奖' if number ==123456 else '遗憾')
多分支结构 :if elif else
多个条件连接: if a and b; if c or d
二、循环结构
1.遍历循环 for
for i in range(1,11):
print(i)
# range()遍历[m,n)
2.while无限循环模拟用户登录
i = 1
while i<=3:
user_name = input('please tell me your name:')
pwd = input('please tell me the password:')
if user_name =='xxx' and pwd =='111':
print('wow,pass!')
break
elif i<3:
print('wrong, you only have',3-i,'time(s)')
else:
print('aho,wrong,yingyingying')
i+=1
3.嵌套循环打印菱形
row = eval(input('please tell the row:'))
while row%2==0:
print('wrong,please tell the row again:')
row = eval(input('please tell the row:'))
# 上半部分
top_row =(row+1)//2
for i in range(1,top_row+1):
for j in range(1,top_row-i+1):
print(' ',end='')
for k in range(1,i*2):
print('*',end='')
print()
# 下半部分
bottom_row = row//2
for i in range(1,bottom_row+1):
for j in range(1,i+1):
print(' ',end='')
for k in range(1,2*(bottom_row-i+1)):
print('*',end='')
print()
4.continue
1——100 偶数和
s = 0
i = 1
while i<=100:
if i%2==1:
i+=1
continue
s+=i
i+=1
print(s)
s = 0
for i in range(1,101):
if i%2==1:
continue
s+=i
print(s)
5.10086系统
answer ='y'
while answer =='y':
print('-----------Welcome to 10086------------')
print('1,查询余额')
print('2,查询剩余流量')
print('3,查询当前剩余通话时长')
print('0,退出系统')
choice = input('请输入您要执行的操作:')
if choice=='1':
print('当前余额为:37.3')
elif choice=='2':
print('当前剩余流量为:3GB')
elif choice=='3':
print('当前剩余通话时长:101分钟')
elif choice =='0':
print('退出,感谢您的使用')
break
else:
print('输入有误,请重新输入')
answer = input('还继续操作吗?y/n')
else:
print('退出,感谢您的使用')
三、产生随机数
# 产生1——100之间的随机数
import random
rand = random.randint(1,100)
print(rand)