IF......ELSE Basic structure:
If condition: Do something Else Do something |
Or
If condition: Do something elif condition: Do something Else Do something |
Python is indented to determine the scope of a piece of code, the same level of code indentation to be consistent, the official recommendation is 4 spaces.
tip: Indentationerror, there are indentation errors in the code.
Small exercise:
# Guess Luck number,n=6
# Guess the number is bigger than 6, hint a little bit
# Guess the number is smaller than 6, hint bigger
#猜的数字等于6, Tip Bingo
Code:
1 #!user/bin/env Python32 #-*-CODING:GBK-*-3Luck_num = 194input_num = Int (input ("Please input the luck num:"))5 ifInput_num = = 19:6 Print("bingo!")7 elifInput_num >Luck_num:8 Print("The num is bigger!")9 Else:Ten Print("The num is smaller")
Introduce loop while, add number limit, code:
1 #!user/bin/env Python32 #-*-CODING:GBK-*-3Luck_num = 194input_num = Int (input ("Please input the luck num:"))5 ifInput_num = = 19:6 Print("bingo!")7 elifInput_num >Luck_num:8 Print("The num is bigger!")9 Else:Ten Print("The num is smaller")
For loop mode:
1 forNum_of_timesinchRange (3):2 #When failure exit after three times.3input_num = Int (input ("Please input the luck num:"))4 ifInput_num = = 19:5 Print("bingo!")6 Break7 elifInput_num >Luck_num:8 Print("The num is bigger!")9 Else:Ten Print("The num is smaller") One Else: A Print("You try too many times.")
Three conditional judgments and loops of Python Foundation