Conditional control statements and cases

if statement

  • Basic usage of if statement
if 表达式:
      语句块

The expression can be a simple Boolean value or variable, or it can be a comparison expression or a logical expression (for example: a>b and a!=c). If the expression is true, the statement block is executed: if the expression If false, skip the statement block and continue to execute the following statement.

a=1
b=2
if(a<b)
   print(a+b)


The basic usage of and means and or means or if...else statement is as follows:

if 表达式: 
	 语句块 1 
else: 
	语句块 2

When using the if...else statement, the expression can be a simple Boolean value or variable, or a comparison expression or a logical expression. If the condition is met, the statement block after the if is executed, otherwise, the statement block after the else is executed . When using the else statement, else must not be used alone, it must be used together with the reserved word if.
if...elif...else statement

  • The basic usage of if...elif...else statement is as follows:
if 表达式 1: 
	语句块 1 
elif表达式 2: 
	语句块 2 
elif表达式 3: 
	语句块 3
else: 
	语句块 n 

When using the if...elif...else statement, the expression can be a pure Boolean value or variable, or a comparison expression or a logical expression. If the expression is true, the statement is executed; and if the expression is false, then jump After this statement, proceed to the next elif judgment. Only when all expressions are false, the statement in else will be executed.
While loop

The while loop will continue to execute as long as the condition is true.
Example
As long as i is less than 10, print i:

i = 1
while i < 10:
  print(i)
  i += 1

Note: Remember to increase i, otherwise the loop will execute forever.

Next is the sample code

  • The first game rock paper scissors, this is the demo code of if elif else
import random
print('请输入石头剪子布')
print('石头为0 剪子为1 布为2')
user=int(input('enter: '))
computer=random.randint(0,2)
if(user == 0 and computer == 0):
	print("平局")
elif(user == 0 and computer == 1):
	print("人获胜")
elif(user == 0 and computer == 2):
	print("计算机获胜")
elif(user == 1 and computer == 0):
	print("计算机获胜")
elif(user == 1 and computer == 1):
	print("平局")
elif(user == 1 and computer == 2):
	print("人获胜")
elif(user == 2 and computer == 0):
	print("人获胜")
elif(user == 2 and computer == 1):
	print("计算机获胜")
elif(user == 2 and computer == 2):
	print("平局")

  • Determine the prime number
a=int(input('enter:'))
for i in range(2,a):
    if a%i==0:
        print('不是质数')
        break
    else:
        print('a是质数')
        break

You must add break, otherwise the big numbers will keep looping

  • Output the number of daffodils within 1000
i = 100
a = 0 
b = 0 
c = 0 
number =0
print('1000的以内水仙花数:')
while i < 1000:
    a = i //100  
    b = (i - a *100 ) // 10 
    c = (i % 10)  
    if i == a ** 3 + b ** 3 + c ** 3 :
        print(i) 
    i += 1

If you think the writing is good, give attention to the ball!

Guess you like

Origin blog.csdn.net/h123456789999999/article/details/113177488