First, a simple for loop
1 repeat the same thing
for looper in [1, 2, 3, 4, 5]: print("hello")
Looper value of 1 from a beginning, the looper = 1
2 corresponds to each value in the list, all of this work this cycle will complete a cycle block
Before each execution cycle block 3, variable looper will assign to the next value in the list
Counting cycle : a certain number of repeated cycles, called counting loop
2 Repeat slightly different things
Each time a for loop to do different things
for looper in [1, 2, 3, 4, 5]: print(looper)
Brackets what to do with? python is a comma between the use and the number of brackets to create a list
The cyclic meaningful things to do: print multiplication table 9:
for looper in [1, 2, 3, 4, 5, 6, 7, 8, 9]: print(9,"*",looper, "=", 9 * looper)
3 no digital count:
for name in [ " Li " , " Wang " , " Li " ]: Print (name, " said: good students " )
Second, a shortcut: range () function
1, rewriting the multiplication of Table 9:
for looper in range(1, 10): print(9,"*",looper, "=", 9 * looper)
2, range () function is common usage:
Usually count mode, starting from 0, when we conduct computer operations, usually at zero.
for i in range(0, 5): print("hello")
3, range () shorthand
for i in range(5): print("hello")
Third, out of the loop
break
for name in [ " Li " , " Wang " , " Li " ]: Print (name, " said: good students " ) IF name == " Li " : BREAK
Fourth, the string formatting
" {}, He said: good students " .format ( " Li " )
Rewrite the preceding code:
for name in [ " Li " , " Wang " , " Li " ]: Print (name, " said: good students " )
V. Summary:
1 for loop
2 range () function, a loop count shortcut
3 out of the loop Keywords: break
4 String Format "" .format ()
Sixth, after-school practice
1. Write a program that displays a multiplication table. Asked multiplication table which displays the number of users at the beginning of the interaction is as follows:
Asked: Which number multiplication table you want to print:
3 user input, the print multiplication table 3, the user input 5, 5 prints multiplication table
Output is as follows:
5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
2 original multiplication table to add a function, when the user enters a number of multiplication table to print complete, and then ask to be multiplied to a few? Then according to the user's input, prints out the multiplication table.
Interaction is as follows:
Asked: Which number you want to print a multiplication table
A number of user input (assuming that the user inputs 6)
The system then asked: maximum multiplier to a few
Take the user input to a few maximum (assuming that the user inputs to take the maximum 10)
Output System as follows:
6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 6 * 7 = 42 6 * 8 = 48 6 * 9 = 54 6 * 10 = 60
3 guessing game, players guess a 1-100 number, a total of six times the number of opportunities to guess, guess every time the system will prompt big or small than the number generated, guess prompt the player wins, the number of calculations wrong guess If up to 6 times, prompt the player loses.
Tip: The secret to generate the number of available following statement:
import random secret = random.randint(1, 100)
Seven Answers
1
int = NUM (INPUT ( " you want to print the table of which the number of multiplication: " )) for I in Range (. 1,. 11 ): Print ( " {} * {} = {} " .format (NUM, I, NUM * i))
2
sel_num = int (the INPUT ( " you want to print a multiplication table which number: " )) max_num = int (the INPUT ( " maximum multiplier to a few: " )) for i in the Range (1, max_num + 1 ): Print ( " { *} = {{}} " .format (sel_num, I, sel_num * I))
3
Import Random Secret = random.randint (1, 100 ) Print ( " Please guess a 1-100 number, you have 6 chances " ) Success = 0 for i in the Range (6 ): GUESS = int (the INPUT ( " Please guess the number: " )) IF gUESS < Secret: Print ( " guess the number is too small " ) elif gUESS> Secret: Print ( " guess the number is too big " ) the else : Success1 = BREAK IF Success == 1 : Print ( " Congratulations, you guessed it " ) the else : Print ( " I'm sorry, you guess wrong, the secret number: " , Secret)