if...else...loop:
if conditions are met:
statement to execute
else:
statement to execute
age = input( " Please enter your age: " ) age_num = int(age) if age_num >= 18 : print ( " Congratulations, you are an adult, you can do what adults can do " ) else : print ( " Don't worry, boy, you have to wait a few more years " )
while loop:
while conditions are met:
statement to execute
i = 1 while i <= 100: print("%d"%i) i = i+1
Simple little exercise 1:
sex = input( " Please enter your gender: " ) if sex== " Male " : height =input( " Please enter your height (cm): " ) salary =input( " Please enter your salary: " ) beau =input( " Are you handsome? " ) height_num = int(height) salary_num = int(salary) if height_num >= 180 and salary_num >= 1000000 and beau == " handsome " : print ( " Hello, Gao Fushuai " )
if sex== " female " : skiny_color =input( " Are you white? " ) salary =input( " Please enter your salary: " ) beau =input( " Are you beautiful? " ) salary_num = int(salary) if skiny_color >= "白" and salary_num >= 1000000 and beau == "美" : print ( " Hello, Bai Fumei, can you be a friend? It's the kind that is very good kind " )
Simple Exercise 2: Printing the Nine-Nine Multiplication Table
i = 1 while i <= 9: j=1 while j <= i: print("%d*%d=%d\t"%(j,i,j*i),end="") j+=1 print("") i += 1
Simple Exercise 3: Rock Paper Scissors Game
Introduce the random function, random.randint(0,1): Randomly obtain integer numbers between 0 and 1
import random #Prompt and get user input player=int(input( " Please input: 0 scissors 1 rock 2 cloth: " )) compter = random.randint(0,2 ) # Judge the user's input, and then display the corresponding result # if the player wins the condition: if (player == 1 and compter ==0) or (player == 2 and compter == 1) or (player == 0 and compter == 2 ): print ( " Congratulations, you won " ) # elif conditions for a draw: elif player == compter: print ( " It's a draw, wash your hands, the final battle is over Dawn " ) else : print ( " Sorry, you lost ")
Practice it yourself. . . . . .