Python Part 2 流程控制

一、分支结构

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)

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值