Python ''control flow'', python Control Flow
I. if statement
Format:
i1 = 3if i1 > 4: print('yes you are right')elif 0 < i1 < 4: print('im dont konw')else: print('no you are wrong')
Note the (:) symbol after if, elif, and else. We use it to tell Python to follow a statement block.
Ii. while statement
Number = 23 running = Truewhile running: guess = int (input ('enter an integer: ') if guess = number: print ('Congratulations, you guessed it ') running = False elif guess <number: print ('No, it is a little lower than that ') else: print ('No, it is a little higher than that') else: print ('the while loop is over') print ('done ')
Output result:
Enter an integer: 22
No, it is a little lower than that
Enter an integer: 24
No, it is a little higher than that
Enter an integer: 23
Congratulations, you guessed it
The while loop is over
Done
Note: In Python2.x, raw_input is used, and in Python3.x, input is used. And it must be followed by a colon.
3. for Loop
Format: for... in
For I in range (): print (I) else: print ('the loop is over') Result: C: \ Python36 \ python.exe C: /Users/CAI Rui/7. py1234the loop is overProcess finished with exit code 0
Note: range (1, 5) Only outputs 1-4 but not 5. Also, the else part is optional. If else is included, it is always executed once after the for loop ends, unless a break statement is encountered.
The for loop is recursive within this range, which is equivalent to assigning each number (or object) in the sequence to I, one at a time, and then executing the program block with each I value.
Programming is fun
When the work is done
If you wanna make your work also fun:
Use Python