while循环
while 条件 :
代码块inp_age = input('>>>Please input the age :')inp_age = int(inp_age)if inp_age < 18: print("it's too small!")elif inp_age == 18: print('congratulatin!!!')else: print("it's too big!!!")
>>>Please input the age :23it's too big!!!
while True: inp_age = input('>>>Please input the age :') inp_age = int(inp_age) if inp_age > 18: print("it's too big!!!") elif inp_age == 18: print('Congratulations!!!') break else: print("it's too samll!!!")
>>>Please input the age :16it's too samll!!!>>>Please input the age :19it's too big!!!>>>Please input the age :18Congratulations!!!
# 三条命life = 0count = 3while life < count: inp_age = input('>>>Please input the age :') inp_age = int(inp_age) if inp_age < 18: print("it's too small!!!") elif inp_age == 18: print('Congratulation!!!') break else: print("it's too big!!!") life += 1
>>>Please input the age :16it's too small!!!>>>Please input the age :19it's too big!!!>>>Please input the age :20it's too big!!!
# if , if ...else,if... elif....else ...的嵌套# 一次猜对得金牌,二次猜对得银牌,三次得铜牌life = 0count = 3while life < count: inp_age = input('>>>Please input the age :') inp_age = int(inp_age) if inp_age < 18: print("it's too small!") elif inp_age == 18: if life == 0: print('Gold medal!!!') elif life == 1: print('Silver medal!!!') else: print('Bronze medal!!!') break else: print("it's too big!") life += 1
>>>Please input the age :17it's too small!>>>Please input the age :18Silver medal!!!
# 奖品可以选择life = 0count = 3award_dict = {0: 'an apple', 1: 'an orange', 2: 'a banana'}while life < count: inp_age = input('>>>Please input the age :') inp_age = int(inp_age) if inp_age > 18: print("it's too big!") elif inp_age == 18: print(f'Congratulations!!! \nPlease choose your award:{award_dict}') choice = input('>>>Please input the number :') choice = int(choice) get_award = award_dict[choice] print(f'Your award is {get_award} ') break else: print("it's too small!") life += 1
>>>Please input the age :19it's too big!>>>Please input the age :18Congratulations!!! Please choose your award:{0: 'an apple', 1: 'an orange', 2: 'a banana'}>>>Please input the number :1Your award is an orange
# 选择多个奖品life = 0count_of_game = 3count_of_award = 0award_dict = {0: 'an apple', 1: 'an orange', 2: 'a banana'}while life < count_of_game: inp_age = input('>>>Please input the age :') inp_age = int(inp_age) if inp_age < 18: print("it's too small!") elif inp_age == 18: print(f'Congratulations!!! \nPlease choose your award: {award_dict}') while count_of_award < 3: choice = input('>>>Please input the number :') choice = int(choice) get_award = award_dict[choice] print(f'Your award is {get_award}!') count_of_award += 1 break else: print("it's too big!") life += 1
>>>Please input the age :20it's too big!>>>Please input the age :18Congratulations!!! Please choose your award: {0: 'an apple', 1: 'an orange', 2: 'a banana'}>>>Please input the number :1Your award is an orange!>>>Please input the number :0Your award is an apple!>>>Please input the number :2Your award is a banana!
while + continue(掌握)
# 计算0+1+2....+99+100i = 0sum = 0while i < 100: i += 1 sum += iprint(sum)
5050
# continue 不执行下面的代码,跳出本次循环,进行下次循环。i = 0sum = 0while i < 100: i += 1 if i == 20 or i == 30: continue sum += iprint(sum)
5000
continue 与 break
break直接结束while循环,如果循环100次,在50次时使用break,后面50次不在循环,结束本层循环
continue如果循环100次,在50次时使用continue,第50次不循环,第51次继续循环,结束本次循环。while + else 以后尽量不要使用
如果while循环结束,且没有被break,则执行else内代码。
i = 0while i < 5: i += 1 if i == 4: breakelse: print("I'm not breaking!")print(i)
4
i = 0while i < 5: i += 1 if i == 6: breakelse: print("I'm not breaking!")print(i)
I'm not breaking!5
for循环
while循环可控,但是控制不好容易死循环。
my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']print(my_info_list[0])print(my_info_list[1])print(my_info_list[2])print(my_info_list[3])
nameheightweight['running', 'reading']
my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']i = 0while i < len(my_info_list): print(my_info_list[i]) i += 1
nameheightweight['running', 'reading']address
my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']for i in my_info_list: print(i)
nameheightweight['running', 'reading']address
# for + breakmy_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']for i in my_info_list: if i == 'weight': break print(i)
nameheight
# for + continuemy_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']for i in my_info_list: if i == 'weight': continue print(i)
nameheight['running', 'reading']address
# for ...else...如果for循环没有被break,则执行else代码。my_info_list = ['name', 'height', 'weight', ['running', 'reading'], 'address']for i in my_info_list: if i == 10: break print(i)else: print("I'm not breaking!")
nameheightweight['running', 'reading']addressI'm not breaking!
while 和 for 循环的异同
相同点:都是循环
不同点:while需要条件,for循环不需要for循环实现loading
import timeprint('loading', end='')for i in range(6): print('.', end='') time.sleep(0.3)
loading......
登陆
username = 'William'password = '123'count = 0choice = 0function_dict = {0: 'reading', 1: 'watching', 2: 'chatting', 3: 'logout'}while count < 3: inp_username = input('>>>Please input your name:') inp_password = input('>>>Please input your password:') if inp_username == username and inp_password == password: re_password = input('>>>Please input your password again:') if re_password == inp_password: print( f'login successfully! \nPlease choose the function:{function_dict}') while True: inp_function = int(input('>>>Please choose:')) function = function_dict[inp_function] if inp_function == 3: print('You are logout!') count = 5 break else: print(f'You are {function}') else: print('Passwords are different!') else: print('Username or password is incorrect!') count += 1
>>>Please input your name:William>>>Please input your password:123>>>Please input your password again:123login successfully! Please choose the function:{0: 'reading', 1: 'watching', 2: 'chatting', 3: 'logout'}>>>Please choose:0You are reading>>>Please choose:1You are watching>>>Please choose:1You are watching>>>Please choose:2You are chatting>>>Please choose:3You are logout!