1. If format
>>> 1<3
True True
>>> 1>3
False false
If condition: Condition +:
(tab) Execution statement
(tab) Execute Statement Front tab for if condition execution Trun (TRUE)
.....
Else
(tab) Execution statement execution false (false)
.......
Determine if the input number is 8
1 Print('Hi')2temp= input ("Number ?")3guess=Int (temp)4 ifGuess==8:5 Print("Yes")6 Else:7 Print(" on")8 Print(" the")9
Add Size tip Note indent
1 Print('Hi')2temp= input ("Number ?")3guess=Int (temp)4 ifGuess==8:5 Print("Yes")6 Else:7 ifGuess>8:8 Print("+")9 Else:Ten Print("-") One Print(" the") A
2.while
While condition:
Execute when condition is true (true)
Limit times
1 Print('Hi')2temp= input ("Number ?")3guess=Int (temp)5 whileGuess! = 8:6temp= input ("NoNoNo")7guess=Int (temp)8 ifGuess = = 8:9 Print("Yes")Ten Else: One ifGuess>8: A Print("+") - Else: - Print("-") - Print(" the")
3. And OR
A line in Python3 can write multiple statements, separated by ";".
Lift example: a = 4;c = 5
A statement in Python3 can be divided into multiple lines of writing, using a backslash ' \ ' or using parentheses to break up into several lines
Print (' I love \
Python ')
Or
>>> (I Love and
Python)
Print\
("HH")
Random number requires random module, Random.randint (A, A, b)
Give users three chance to try, run out of opportunities or user guess answers all exit loops
1 ImportRandom2Times = 33Secret = Random.randint (1,5) 4 Print("--------Welcome to guessing the numbers game--------\ n") 5Guess =06 Print("guess which value in 1-5? :", end=" "Print prints a line by default and ends with a newline. End= "" means the end does not wrap, plus a space7 while(Guess! = secret) and(Times >0):8Guess =int (input ()) is the same as C's scanf and does not output a string9Times = Times-1Ten ifGuess = =Secret: One Print("\ n Guess right, why are you so interested?!! \ n") A Print("but there's no reward for guessing.") - Else: - ifGuess >Secret: the Print("It's big, ~\n.") - Else: - Print("Oh, little ~\n.") - ifTimes >0: + Print("Try one more time:", end=" ") - Else: + Print("3 times the chance is exhausted! ~\n") A Print("game over, don't play ^_^ \ n")
1 Print ('--------print a list of numbers-----------') 2 tmp = input (' Please enter a number:')3 num = int (tmp)4 i = 15 while num:6 print(i)7 i = i + 18 num = num-1
Run results, enter 5
--------Print a list of numbers-----------
Please enter a number: 5
1
2
3
4
5
1 Print('--------Print a set of symbols-----------')2TMP = input ('Please enter the number of groups:')3Fuhao = input ('Please enter a symbol:')4num =INT (TMP)5 whileNum:6 Print(' '* num + Fuhao *num) repeats num times spaces with num times symbols7num = num-1
--------print a set of symbols-----------
Please enter the number of groups: 3
Please enter a symbol: 0
000
00
0
Write a program that determines whether a given year is a leap years.
1temp = input ('Please enter a year:')2 while nottemp.isdigit ():3temp = input ("Sorry, your input is wrong, please enter an integer:")4 5Year =Int (temp)6 ifyear/400 = = Int (year/400):7 Print(temp +'is a leap year! ')8 Else:9 if(YEAR/4 = = Int (YEAR/4)) and(year/100! = Int (year/100)):Ten Print(temp +'is a leap year! ') One Else: A Print(temp +'not a leap year! ')
Python if while