# while loop # Format : # Print 100 All over hello,python # limit :100 Time -- Conditions : Conditions for ending # 1.2.3.4.5.6.7............100 # # Once in the while loop , Then you must consider the scenario where the condition does not hold # Number of Representatives count = 1 while count < 101: # Code that will execute only when the condition is met print('hello,python') # It's very important , There must be an operation , In some cases, it can make the condition not tenable ! count += 1 # while The loop does not specify the number of cycles count = 1 nums = input(' Input times :') # The number of entries while count <= int(nums): # Limited number of times print(count) count += 1 # Code that will execute only when the condition is met print('hello,python') # Avoid the dead cycle : # 1. Is that the circular condition does not hold # 2. In circulation , Meet other conditions , Exit loop # # Treatment of dead cycle :break/continue # break : If the condition is satisfied, the current cycle will be terminated count = 1 nums = input(' Input times :') # The number of entries while count <= 100: # Limited number of times print(count) print('hello,python') # Inside the loop , If there are other conditions , If satisfied , Exit the loop directly if count ==50: print(' already 50 Time , bye ') break # Exit the loop directly count += 1 # Code that will execute only when the condition is met # # continue: Abort the current cycle , Start the next cycle count = 1 # nums = input(' Input times :') # The number of entries while count <= 10: # Limited number of times print(count) count += 1 # Code that will execute only when the condition is met if count == 2: continue # Skip the current loop , Enter the next cycle print('hello,python') # Inside the loop , If there are other conditions , If satisfied , Exit the loop directly # if count ==5: # print(' already 50 Time , bye ') # break # Exit the loop directly # # # break and continue The difference between : Resignation (break) And asking for leave (continue) The difference between # print(*************************--*******************************************) # Focus on mastering :for loop # for loop # Traverse : From a to Z , Each member visits once . # member : Lists and dictionaries -- Most commonly used # Traversal of list list_p = ['mr Zhang ',' For you ','madao',' Wu queer ',' Luo Zhi '] ''' Format : for Variable in list : When you get a member , All the code that goes back to execution In this list , Go get every member , Assign a value to a variable Variable = The first member Variable = The second member Variable = Third member Variable = The fourth member Number of cycles = The length of the list ''' # Traverse the values of the list for item in list_p: # Get the values of all lists print(item) # Print each member if item == ' Wu queer ': # If the specified value is obtained break # Just stop the cycle for item in list_p: # Get the values of all lists if item == ' Wu queer ': # If the specified value is obtained continue # The value of this line will not be printed , Skip to the next round print(item) # Print each member # Traverse through the index of the list : # range: Built in functions , function : Produce integer word order , There are three types : # The starting point : The default is 0, # End : Self determination # step : The default is 1 # With start , The end point is not included # 1.range(n): Generate a... By default 0 To n-1 Integer sequence of # range(5)[0,1,2,3,4] # 2.range(n,m): Generate a... By default 0 To m-1 Integer sequence of # range(1,5)[1,2,3,4] # 3.range(n,m,k): Compared with other functions for loop .n Represents the initial value ,m Indicates the end value ,k Indicating step size . # An initial value of n, The end value is m, A sequence of decreasing or increasing integers # range(1,10,2)[1,3,5,7,9] # Traverse the index of the list # The length of the obtained list is :6 # The index to traverse is :0.1.2.3.4.5 for index in range(len(list_p)): print(index) # Print subscript print(list_p[index]) # Print value # Traversal of dictionaries person_info = {'sex':' male ','tzh':' good-looking , handsome , Money goes into ','age':30} # The first is by traversing the key names : for key in person_info.keys(): print(key) print(person_info[key]) # The second is by traversing key value pairs : for key,value in person_info.items(): print(key,value) # double for loop # 1. Want to know , What is the outer layer , What is the inner layer # 2. In writing double for # 3. According to the output , Adjust the statement logic ''' Output the graph 1 1 2 1 2 3 1 2 3 4 first line : Output 1 range(1,2) Row number +1 The second line : Output 1,2 range(1,3) The third line : Output 1,2,3 range(1,4) In the fourth row : Output 1,2,3,4 range(1,5) ''' for index in range(1,5): print(' The first {} That's ok :'.format(index)) for sub in range(1,index+1): print(sub,end=' ') # end='' No line breaks print('') # Homework after class # 1、 A mall is cutting prices to promote sales , All original prices are integers ( Floating point situations do not need to be considered ), If the purchase amount 50-100 element ( contain 50 Genna 100 element ) Between , Will give 10% A discount of , # # If the purchase amount is greater than 100 Yuan will be given to you 20% discount . Write a program , Ask about the purchase price , Then show the discount (%10 or 20%) And the final price . print(' The first question is ************************************************') price = int(input(' The price of this article is :')) # There is a price entered if 50 <= price <= 100: # Conditions 1 Is the purchase amount 50-100 element ( contain 50 Genna 100 element ) Between price = 0.9 * price # The depreciated price # print(' I can call you here 10% A discount of , The discount for you is :',price,' Thank you for your patronage !') # The output condition is satisfied 1 Of The first method print(' I can call you here 10% A discount of , The discount for you is :{:.2f}'.format(price)) # The second method elif price > 100: price = 0.8 * price # print(' I can call you here 20% A discount of , The discount for you is :',price,' Thank you for your patronage !' ) # The output condition is satisfied 2 Of The first method print(' I can call you here 20% A discount of , The discount for you is :{:.2f}'.format(price)) # The second method else: print(' You are not satisfied with the discounted price , Please continue to buy !') # Not meeting the conditions 1 The conditions of execution # 2 Determine if it's a leap year # Tips : # Enter a valid year ( Such as :2019), Determine if it's a leap year ( There is no need to consider non numerical cases ) # If it's a leap year , Then print “2019 Year is a leap year ”; Otherwise print “2019 Year is not a leap year ” # What is leap year , Please find out for yourself ( The requirements document does not explain ) # # Please enter a year # Conditions 1: Can't be 100 Divisible and capable of being divided by 4 to be divisible by # Conditions 2: Enough quilt 100 Divisible and capable of being divided by 400 to be divisible by print(' The second question is ************************************************') year = int(input(' Please enter a year :')) # Enter an integer year if (year % 100 != 0 and year % 4 == 0) or (year % 100 == 0 and year % 400 == 0): # Conditions 1 And conditions 2 print(' What you entered ' + str(year) + ' It's a leap year ') # To be satisfied is to output this else: # conversely print(' What you entered ' + str(year) + ' It's not a leap year ') # Can not be satisfied is output this # 3. Find the maximum of three integers # Tips : Definition 3 A variable print(' Third question ************************************************') numb1 = input(' Please enter the first number :') # Enter the first variable numb2 = input(' Please enter the second number :') # Enter the second variable numb3 = input(' Please enter the third number :') # Enter the third variable max = 0 # Assumed maximum max by 0 if numb1 > numb2: # advanced 2 The number is worth comparing max = numb1 # The maximum value is numb1 else: # conversely max = numb2 # The maximum value is numb2 if max > numb3: # Compare max Compare with the remaining values print(' The maximum value is '+ str(max)) # The maximum value is max Then print this else: # conversely print(' The maximum value is ' + str(numb3)) # The maximum value is numb3 Then print this # 4, Use for Print the multiplication table # Tips : Output the multiplication table , The format is as follows :( There is a space between each item of data Tab key , have access to "\t") print(' Fourth question **********************************************') for index in range(1,10): # The value range is (1-9) for numb in range(1,index+1): # Each cycle needs to add 1 print('{}*{} = {}\t'.format(index, numb, index*numb),end = ' ') # Use format Function to output print('') # Advanced work ''' Use the cycle to complete the scissors, stone and cloth game , Prompt the user to enter the punch to be played : stone (1)/ scissors (2)/ cloth (3)/ sign out (4) The computer randomly punches the winner , Show user wins 、 Negative or draw . Run as shown in the figure below : Tips : The computer punches at random Use random number , First, you need to import the module of random numbers —— “ tool kit ” import random After importing the module , Can be directly in Module name Knock on the back "." Then press Tab key , All functions included in the module will be prompted random.randint(a, b), return [a, b] Integer between , contain a and b ''' print(' The first question is *********************************************************************************************') import random # Module for importing random numbers while True: player = int(input(' Please enter the options you want stone (1)/ scissors (2)/ cloth (3)/ sign out (4):')) # Enter the selected option if player == 4: # If the player enters 4 , Meet the conditions , Execute this print(' Game exit ') break # Exit the current loop computer = random.randint(1,3) # The computer is randomly placed in 1-3 Choose between if ((player == 3 and computer == 1) # If the player gives out cloth (3) And the computer (1) Players win or (player == 1 and computer == 2) # If the player throws a stone (1) And computer scissors (2) Players win or (player == 2 and computer == 3)): # If the player gives scissors (2) And the computer (3) Players win print(' The player's choice of punch is {}, Computer punches {}, Players win !'.format(player, computer)) # Use format Function to print the player's victory elif player == computer: # If the player and the computer are the same , It's a draw print(' The player's choice of punch is {}, Computer punches {}, It's a draw !'.format(player, computer)) # Use format Function to print a draw else: # conversely print(' The player's choice of punch is {}, Computer punches {}, The player failed !'.format(player, computer)) # Use format Function to print out that the player failed print(' Game over ') # 2、 Write the following program # a. User input 1-7 Seven numbers , They represent Monday to Sunday # b. If input 1~5, Print the corresponding “ Monday ”~“ Friday ”, If the number entered is 6 or 7, Printout “ Over the weekend ” # c. If input 0, Exit loop # d. Type in something else , Tips :“ Incorrect input , Please re-enter !” # Tips : This question can be used if and while loop , At the same time, it is necessary to verify whether the user's input is correct . Do not consider floating-point numbers, etc . print(' The first 2 topic *********************************************************************************************') my_days = [" Monday ", " Tuesday ", " Wednesday ", " Thursday ", " Friday ", " Saturday ", " Over the weekend "] # Define a list while True: num = input(" Please enter 0~7 Number in range :") # Input number if num in list("1234567"): # If the entered value is in the list , Meet the conditions 1, Perform the next step print(" It's today {}!".format(my_days[int(num)-1])) # Use format function elif num == "0": # The value entered is equal to 0, Meet the conditions 2 print(" Program exit !") # Print break # Exit the current loop else: # conversely , Input does not belong to 0-7 Number of ranges , conditions for execution 3 print(" Incorrect input , Please re-enter !") # Print # 3、 Bubble sort ( There is no requirement to submit , Memorize before the interview ) # Use the loop to implement the sorting algorithm ( Bubbling , Select an algorithm , Please find out for yourself .) # Tips : utilize for loop , complete a=[1,7,4,89,34,2] Sort ( Small numbers come first , Behind the big row ), Out of commission sort、sorted And other built-in functions or methods a =[1,7,4,89,34,2] for k in range(1,len(a)): for m in range(0,len(a)-k): if a[m] > a[m+1]: a[m],a[m+1] = a[m+1],a[m] print(a)