深浅copy与while循环

深浅copy与while循环

copy分为浅copy与深copy

浅copy: 是把原列表第一层的内存地址不加以区分完全copy一份给新的列表

list1 = ['egon'.'lxx',[1,2]]
list3=list1.copy()
print(list3)
》》['egon','lxx',[1,2]]
list1[o] = 'EGON'
list1[1] = 'LXX'
list1[2][0] = 111
list1[2][1] = 222
print(list1)
》》['EGON','LXX',[111,222]]
print(list3)
》》['egon','lxx',[111,222]]

通过以上实验可以得出,要想copy得到新的列表与原列表的操作完全独立开,必须有一种可以区分可变类型与不可变类型的copy机制,这就是深copy

深copy:

ist1 = ['egon'.'lxx',[1,2]]
list3=list1.copy()
print(list3)
》》['egon','lxx',[1,2]]
list1[o] = 'EGON'
list1[1] = 'LXX'
list1[2][0] = 111
list1[2][1] = 222
print(list1)
》》['EGON','LXX',[111,222]]
print(list3)
》》['egon','lxx',[1,2]]

while循环

username = 'egon'
password = '123'
while True:
	inp_name = input('请输入你的账号')
	inp_pwd = input('请输入你的密码')
	if inp_name == username and inp_pwd == password:
		print('登录成功')
	else:
		print('账号或者密码错误')
		
》》请输入您的账号(xxxx)
   请输入您的密码(xxxx)
   .........
   一直循环下去

退出循环的两种方式:

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

username = 'egon'
password = '123'
tag = True
while tag:
	tad = False
	inp_name = input('请输入你的账号')
	inp_pwd = input('请输入你的密码')
	if inp_name == username and inp_pwd == password:
		print('登录成功')
else:
	print('账号或者密码错误')
	
》》请输入你的账号(xxxx)
   请输入你的密码(xxxx)
   #在这里进行下一次循环判断时False就生效了不会继续往下循环
   
   
那如果想要第一次登录成功就停止运行呢
username = 'egon'
password = '123'
tag = True
while tag:
	inp_name = input('请输入你的账号')
	inp_pwd = input('请输入你的密码')
	if inp_name == username and inp_pwd == password:
		print('登录成功')
		tag = False #之后的代码还会运行
	else:
		print('账号或者密码错误')
	print('===end===')
	
》》请输入你的账号(xxxx)
    请输入你的密码(xxx)
    ===end===

二.break------>只要运行到break就会立刻终止本层循环

username = 'egon'
password = '123'
while True:
	inp_name = input('请输入你的账号')
	inp_pwd = input('请输入你的密码')
	if inp_name == username and inp_pwd == password:
		print('登录成功')
		break #立刻终止本层循环,后面的代码也不会运行
	else:
		print('账号或者密码错误')
print('===end===')

》》请输入你的账号(xxx)
   请输入你的密码(xxx)
   登录成功

beark与循环嵌套

username = 'egon'
password = '123'
while True:
	inp_name = input('请输入你的账号')
	inp_pwd = input('请输入你的密码')
	if inp_name == username and inp_pwd == password:
		print('登录成功')
		while True:
			cmd = input('请输入命令')
			if cmd == 'q':
				break
			print('命令{x}正在运行'.format(x == cmd))
		break 
else:
	print('账号或者密码错误')


》》请输入你的账号(xxx)
   请输入你的密码(xxx)   

将条件改为False的方式:

username = 'egon'
password = '123'
tag = True
while tag:
	inp_name = input('请输入你的账号')
	inp_pwd = input('请输入你的密码')
	if inp_name == username and inp_pwd == password:
		print('登录成功')
		while tag:
			cmd = input('请输入命令')
			if cmd == 'q':
				tag = False
			else:
				print('命令{x}正在运行'.format(x = cmd))
				break 
		else:
			print('账号或者密码错误')


》》请输入你的账号(xxx)
   请输入你的密码(xxx)   

用循环跳过一段程序然后继续执行下一段代码

count = 0
while count <6:
	if count == 4:
		count += 1
		continue
	print(count)
	count += 1	

while循环+else:

1.改变条件的方式

username = 'egon'
password = '123'
count = 0
tag = True
while tag:
	if count == 3:
		break
	inp_name = input('请输入你的账号')
	inp_pwd = input('请输入你的密码')
	if inp_name == username and inp_pwd == password:
		print('登录成功')
		while tag:
			cmd = input('请输入命令')
			if cmd = 'q'
				tag = False
			else:
				print('命令{x}正在运行'.format(x = cmd))
		else:
			print('账号或者密码错误')
			count += 1
原文地址:https://www.cnblogs.com/hm666/p/12453256.html