python_条件循环判断(break&continue&import&input)

 循环=迭代=遍历

if 条件判断(变量赋值、input接收的均为字符串):

 

#input 接收到的输入为字符串,所以如果需要对比大小需要先转换类型
score=input('请输入您的成绩:')
score=int(score)
if score>90:
    print('优秀')
elif score>80:
    print('')
elif score>=60:
    print('及格')
else:
    print('不及格')
while条件循环(循环、迭代、遍历):
须提前定义计数,否则为死循环直至py报错
count=0
while count<3:
    print('哈哈')
    count += 1

循环中break和continue的区别案例:

# 代码1:
count=0
while count<10:
     if count==3:
         break
     print('哈哈')
     count+=1
# 代码2:
count=0
while count<10:
     print('哈哈')
     count+=1
     if count==3:
         break

代码1和代码2的效果一样,皆打印三行哈哈

for循环:(数组list循环)

 

money=200
names=['xiaohong','xiaoming','xiaolan','xiaobai']
for name in names:
    if name=='xiaoming':
        continue
    print('给%s发%s元钱'%(name,money))

 

 

import导入模块

 

import random
num=random.randint(1,1000)#随机产生一个整数
print(num)
count=0
while count <3:
    count+=1
    guess=int(input('请输入您猜的数字:'))
    if guess>num:
        print('猜大了')
    elif guess<num:
        print('猜小了')
    else:
        print('恭喜您,猜对了!游戏结束')
        break
else:
    print('次数已用尽,游戏结束')
# 正常结束while循环,会执行else代码;如果是break结束的循环,则不会执行else

用户登录校验:

usernames=['xiaoming','xiaohong','xiaolan']
passwords=['123456','abc123','1234@qwer']
import datetime
today=datetime.datetime.today()
for i in range(3):
    username=input('username:').strip()
    password=input('password:')
    password=password.strip()#去掉字符串两边的空格
    if username=='' or password=='':
        print('账号/密码不能为空!')
    # elif usernames.count(username)==0:判断元素的个数
    elif username not in usernames:#判断远古三是否在list中
        print('用户不存在!')
    else:
        user_index=usernames.index(username)
        p=passwords[user_index]
        if password==p:
            print('欢迎%s登录,今天的日期是%s。'%(username,today))
            break
        else:
            print('密码错误!')
else:
    print('次数已用尽!')

 

 

 

 

 


posted @ 2019-05-06 19:08  mcdull0  阅读(529)  评论(0编辑  收藏  举报