while循环和for循环

while循环

定义:

循环语句,计算机的一种基本循环模式。当满足条件时进入循环,不满足跳出

为什么要使用:

重复执行某些命令

使用方法

while 条件:
     代码1
     代码2
     代码3
print(3)

例:

while count < 5: 
    print(count) 
    count+=1 

死循环与效率问题

count=0
while count < 5: # 5 < 5
    print(count) # 0,1,2,3,4

while True:
    name=input('your name >>>> ')
    print(name)

纯计算无io的死讯会导致致命的效率问题

while True:
    1+1

while 1:
    print('xxxx')

退出循环的两种方式

方式一:将条件改为False,等到下次循环判断条件时才会生效
例:

while tag:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')
    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        tag = False 
    else:
        print('账号名或密码错误')
    # print('====end====')

方式二:break,只要运行到break就会立刻终止本层循环

while True:
    inp_name=input('请输入您的账号:')
    inp_pwd=input('请输入您的密码:')

    if inp_name  == username and inp_pwd == password:
        print('登录成功')
        break # 立刻终止本层循环
    else:
        print('账号名或密码错误')

for循环

什么是for循环

循环就是重复做某件事,for循环是python提供第二种循环机制

为什要有for循环

理论上for循环能做的事情,while循环都可以做,之所以要有for循环,是因为for循环在循环取值(遍历取值)比while循环更简洁

如何使用for循环

语法:

for  变量名 in 可迭代对象:# 可迭代对象可以是:列表、字典、字符串、元组、集合
      代码1
      代码2
      代码3
       。。。。

for基本使用之循环取值

列表循环取值

l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
for x in l:  # x='lxx_dsb'
    print(x)
l = ['alex_dsb', 'lxx_dsb', 'egon_nb']
i=0
while i < 3:
    print(l[i])
    i+=1

字典循环取值

dic={'k1':111,'k2':2222,'k3':333}
for k in dic:
    print(k,dic[k])

字符串循环取值

msg="you can you up,no can no bb"
for x in msg:
    print(x)

总结for循环与while循环的异同

1、相同之处:都是循环,for循环可以干的事,while循环也可以干
2、不同之处:
while循环称之为条件循环,循环次数取决于条件何时变为假
for循环称之为"取值循环",循环次数取决in后包含的值的个数\

for+break: 同while循环一样

for+else:同while循环一样

username='egon'
password='123'
for i in range(3):
    inp_name = input('请输入您的账号:')
    inp_pwd = input('请输入您的密码:')

    if inp_name == username and inp_pwd == password:
        print('登录成功')
        break
else:
    print('输错账号密码次数过多')

for+continue

for i in range(3):
    print('外层循环-->', i)
    for j in range(5):
        print('内层-->', j)

补充:终止for循环只有break一种方案

作业

1、编写认证程序,可以登陆成功不同的账号密码
账号密码输错3次则退出

dic = {'egon': '123', 'tank': '234', 'tom': '345'}
count = 0
while count < 3:
    inp_name = input('请输入你的账号: ')
    inp_pwd = input('请输入你的密码: ')
    for name, pwd in dic.items():
        if inp_name == name and inp_pwd == pwd:
            print('登陆成功!')
            count = 3
            break
        else:
            print('账号或密码错误')
            count += 1
            break

1、简述编译型与解释型语言的区别,且分别列出你知道的哪些语言属于编译型,哪些属于解释型

编译型:得全部写完才可以进行编译,可移植性好,运行速度快,但不便于维护跨平台性差
解释型:python php
编译型:go c c++

2、执行 Python 脚本的两种方式是什么

脚本形式  和交互式

3、Pyhton 单行注释和多行注释分别用什么?

 单行注释用井号和单引号  多行用三引号    

4、布尔值分别有什么?

False    True

5、声明变量注意事项有那些?

1、应该见名知意且尽量简短
2、变量名必须是数字、字母、下划线,但是不能以字母开头
3、不能使用关键字及函数命名

6、如何查看变量在内存中的地址?

print(id('变量'))

7、写代码
实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败!

name=input('请输入用户名: ')
pwd=input('请输入密码: ')
if name=='seven' and pwd=='123':
    print('登录成功!')
else:
    print('登陆失败')

实现用户输入用户名和密码,当用户名为 seven 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

count=0
while count<3:
    name = input('请输入用户名: ')
    pwd = input('请输入密码: ')
    if name == 'seven' and pwd == '123':
        print('登录成功!')
        break
    else:
        print('登陆失败')
        count+=1

实现用户输入用户名和密码,当用户名为 seven 或 alex 且 密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次

count = 0
while count < 3:
    name = input('请输入用户名: ')
    pwd = input('请输入密码: ')
    if name == 'seven' or name == 'alex' and pwd == '123':
        print('登录成功!')
        break
    else:
        print('登陆失败')
        count += 1

8、写代码
a. 使用while循环实现输出2-3+4-5+6...+100 的和

count = 2
sm = 0
while count < 101:
    if count % 2 == 0:
        sm += count
        count += 1
    else:
        sm -= count
        count += 1
print(count)

b. 使用 while 循环实现输出 1,2,3,4,5, 7,8,9, 11,12 使用 while 循环实现输出 1-100 内的所有奇数

count = 0
while count < 13:
    if count == 10:
        count += 1
        continue
    print(count)
    count += 1
count=0
while count < 101:
    if count % 2 == 1:
        print(count)
    count += 1

e. 使用 while 循环实现输出 1-100 内的所有偶数

count = 1
while count < 101:
    if count % 2 == 0:
        print(count)
    count += 1

9、现有如下两个变量,请简述 n1 和 n2 是什么关系?

  n1 = 123456
  n2 = n1
n1的地址指向123456的内存地址。n2=n1时就等于n1把123456的地址给了n2,他俩的地址同时指向123456的地址,所以n1、n2的值和id完全一样。

2 作业:编写登陆接口
基础需求:
让用户输入用户名密码
认证成功后显示欢迎信息
输错三次后退出程序

info = {'egon': '111', 'tank': '222', 'alex': '333', 'tom': '444'}
count = 0
while count < 3:
    name = input('请输入账号:')
    pwd = input('请输入密码:')
    for db_name, db_pwd in info.items():
        if name == db_name and pwd == db_pwd:
            print('欢迎登录!')
            count = 3
            break
    else:
        print('账号或者密码错误!')
        count += 1
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容