python流程控制练习

‘’’
if条件
‘’’

示例

sex= ‘female’

age=19

is_beautiful=True

is_successful=True

height=1.70

if sex ==‘female’ and age > 18 and age <20 and is_beautiful \

and height > 1.6 and height < 1.8 :

print(“001”)

if is_successful :

print(‘ok’)

else:

print(‘sb’)

else:

print(‘88’)

#示例2

score=int(input(‘你的成绩:’))

# score=int(score)

if score >= 90:

print(“优秀”)

elif score >= 80:

print(“良好”)

elif score >= 70:

print(“普通”)

else:

print(“很差”)

while循环

tag=True

while tag:

name=input(‘your name is:’)

# pwd=input(‘your pass word is:’)

pwd=int(input(‘your pass word is:’))

if name==‘aaa’ and pwd==111:

# if name == ‘aaa’ and pwd == ‘111’:

print(‘login successful’)

else:

print(‘try again’)

print(">>>>")

while结束循环方式一:

将条件改成false

tag = True

while tag:

name = input(‘your name is:’)

# pwd=input(‘your pass word is:’)

pwd = int(input(‘your pass word is:’))

if name == ‘aaa’ and pwd == 111:

# if name == ‘aaa’ and pwd == ‘111’:

print(‘login successful’)

tag = False

else:

print(‘try again’)

print(">>>>")

while结束方式二 while+continue 结束本次循环,直接进入下一次循环

count = 1

while count < 6:

if count == 4:

count += 1

continue

print(count)

count += 1

# count += 1

‘’‘需求设计一个登陆程序
要求1、输入正确的用户名和密码
要求2、登陆失败后无限次重新输入
要求3、登陆成功后结束程序并打印>>>,’’’

尝试1

name = input(‘your login name:’)

pwd = input(‘your passwords :’)

tap = True

while tap :

if name == ‘bob’ and pwd ==‘111’:

print(‘login successful’)

tap = False

else:

print(‘username or passsword error’)

tap = False

print(’>>>>>’) #都打印>>>,但是只有一次登陆机会

尝试2

while True:

name = input(‘your login name:’)

pwd = input(‘your password :’)

if name == ‘bob’ and pwd == ‘11’:

print(‘login successful’)

break

else:

print(‘user name or password error’)

break

print(’>>>’) #成功打印>>>,失败也打印>>> 但是只有一次尝试机会

尝试3

while True:

name = input(‘please input your login name:’)

pwd = input(‘please input your password :’)

if name == ‘bob’ and pwd == ‘11’:

print(‘login successful’)

break

else:

print(‘user name or password error’)

print(’>>>’) #登陆成功不打印>>>,失败打印>>> 可以无限次重新输入

尝试4

while True:

name = input(‘please input your login name:’)

pwd = input(‘please input your password :’)

if name == ‘bob’ and pwd == ‘11’:

print(‘login successful’,)

print(’>>>’)

break

else:

print(‘user name or password error’)

print(’>>>’)

‘’’
要求1、输入正确的用户名和密码
要求2、只能尝试3次输入错误
要求3、登陆成功后结束程序
‘’’

尝试第一次(运行结果正确,但是程序不停止,错误在while ture,只要更改为while count <4就可以)

count = 1

while True:

if count < 4:

name = input(‘please input your name:’)

pwd = input(‘please input your password:’)

if name == ‘egon’ and pwd == ‘11’:

print(‘login successful’)

break

else:

print(‘username or password error’)

print(’>>>’)

count += 1

count=0

while count < 3:

name=input(‘请输入用户名:’)

password=input(‘请输入密码:’)

if name == ‘egon’ and password == ‘123’:

print(‘login success’)

break

else:

print(‘用户名或者密码错误’)

count+=1

尝试第二次 验证while 循环中嵌套的break会导致else 不运行

count = 1

while True:

if count < 4:

name = input(‘please input your name:’)

pwd = input(‘please input your password:’)

if name == ‘egon’ and pwd == ‘11’:

print(‘login successful’)

break

else:

print(‘username or password error’)

print(’>>>’)

count += 1

else:

print(‘haha’)

尝试第三次 验证将while循环由tap条件终止

count = 1

tap = True

while tap:

if count < 4:

name = input(‘please input your name:’)

pwd = input(‘please input your password:’)

if name == ‘egon’ and pwd == ‘11’:

print(‘login successful’)

tap = False

else:

print(‘username or password error’)

print(’>>>’)

count += 1

else:

print(‘haha’)

‘’’
需求1、输入正确的用户名和密码
要求2、只能尝试3次输入错误,有剩余次数提示
要求3、登陆成功后结束程序
‘’’

count = 3

tag=True

while tag:

if count > 0:

name = input(‘please input your name:’)

pwd = input(‘please input your password:’)

if name == “egon” and pwd == ‘11’:

print(‘login successful’)

break

else:

count -= 1

print(‘username or password error’,\

‘you can try:’,count)

if count==0:

tag=False

练习

1、使用while循环输出1 2 3 4 5 6 8 9 10

‘’'count = 1
while True:
if count != 7:#and count < 11:
print(count)
count += 1

else:

    count += 1
if count == 11:
    break

#该程序虽然最终结果能按要求打印出结果,但是程序逻辑比较混乱。’’’
‘’‘教师版
count = 1
while count < 11:
if count ==7:
count +=1
continue
print(count)
count += 1
#应用continue语法,当条件成立时,停止当次循环,重新开始循环’’’

#2. 求1-100的所有数的和

count = 1

a = 0

while count < 101:

a=a +count

count += 1

print(a)

#3. 输出 1-100 内的所有奇数

count = 1

while count < 101:

if count % 2 == 1:

print(count)

count += 1

1-100所有奇数的和

count = 1

a = 0

while count<101:

if count %2 == 1:

a = count+a

count += 1

print(a)

1~100所有偶数的和

sum = 0

n = 100

while n > 0:

sum = sum + n

n = n - 2

print(sum)

#4. 输出 1-100 内的所有偶数

‘’‘count = 1
while count<=100:
if count %2==0:
print(count)
count += 1’’’

#5. 求1-2+3-4+5 … 99的所有数的和

count = 1

a = 0

while count < 100:

if count % 2 == 1:#99

a = a + count

count += 1

else:

a=a - count

count += 1

print(a)

1~100所有奇数的和

count=1

a=0

while count<100:

if count %2 ==1:

a=a+count

count += 1

print(a)

res=0

count=1

while count < 100:

if count%2 == 0:

res-=count

else:

res+=count

count+=1

print(res)

#6. 用户登陆(三次机会重试)

count = 3

while True:

if count>0:

name = input(‘please input your name:’)

pwd = input(‘please input your password:’)

if name==‘bob’ and pwd==‘11’:

print(‘login successful’)

break

else:

count -= 1

print(‘username or password error’,‘you can try :’,count)

count =3

while count > 0:

name = input(‘请输入用户名:’)

password = input(‘请输入密码:’)

if name==‘bob’ and password == ‘11’:

print(‘登陆成功’)

break

else:

print(‘用户名或密码错误’)

count -= 1

‘’‘教师版
count=0
while count < 3:
name=input(‘请输入用户名:’)
password=input(‘请输入密码:’)
if name == ‘egon’ and password == ‘123’:
print(‘login success’)
break
else:
print(‘用户名或者密码错误’)
count+=1’’’

#7:猜年龄游戏

示范2

tag=True

while tag:

name = input(‘please input youe name:’).strip() #去除字符串里的空白.strip

pwd = input(‘please input your password:’)

if name==‘egon’ and pwd==‘123’:

print(‘login success’)

while tag:

print(’’’

0 退款

1 取款

2 转账

3 查询

‘’’)

choice = input(‘请输入您要执行的操作:’) #choice=‘1’

if choice==‘0’:

tag=False

elif choice==‘1’:

print(‘取款》》》’)

elif choice==‘2’:

print(‘转账》》’)

elif choice == ‘3’:

print(‘查询》》》’)

else :

print(‘输入指令错误,请重新输入’)

else:

print(‘密码错误’)

for i in range(1, 10):

for j

# for j in range(1, 10):

x = i * j

print(’%s * %s = %s’ % (i, j, x), end=’’)

print()

for i in range(1,10):

for j in range(1,i+1):

print(’%s*%s=%s’ %(i,j,i*j)),#end=’ ')

print()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值