Detailed explanation of python conditional statement and while loop example code
- 2021-09-05 00:28:41
- OfStack
02 Conditional Statements and while Loop
3-mesh operation
a = 6
# Original judgment sentence
if a > 5:
print(True)
else:
print(False)
#3 Purpose operation
print(True if a >5 else False)
Logical operation
1. 3 logical operations
And logic and
True on both sides is true
Or logic or
If one side is true, it is true
Non-logical
not logic value is inverted
Priority: not > and > or
2. Logical short circuit
# and Logical short circuit
a = 3 # No, right b Assignment, but the program will not report errors and can run normally
# The left Boolean value is false, and the true or false of the right Boolean value does not affect the whole Boolean value to be false
print(a > 4 and b > 4) # The output Boolean value is False
# or Logical short circuit
a = 3 # No, right b Assignment, but the program will not report errors and can run normally
# The left Boolean value is true, and the true or false of the right Boolean value does not affect the overall Boolean value to be true
print(a > 2 and b > 2) # The output Boolean value is True
# not No logical short circuit
3. Continuous judgment
#python The bottom layer will transform the continuous judgment into and Form of connection
print(1 > 2 > 3) # Equivalent to 1>2 and 2>3, Its value is False
# Logical short circuit of continuous judgment
# Due to and There is a logic short circuit, so there is also a logic short circuit in continuous judgment
# Integer 2 And string '3' Different types can't compare sizes
# Left side 1>2 The Boolean value is false, so there is no need to judge on the right side, so no error will be reported
print(1 > 2 > '3') # The output Boolean value is False
while cycle
# The loop condition can be True , but there must be internal break Ensure that the loop can be terminated, otherwise it will fall into an infinite loop
# Use break The terminated loop is an abnormal ending loop and will not be executed else Part
a = 1
while True:
if a % 5 == 0:
break
print(a)
a += 1
else:
print(' End of loop ')
Exercise
Using while to write 99 multiplication table
# Use continue The terminated loop is not an abnormal ending loop, and will be executed after the loop ends else Part
a = 1
while a < 4:
if a % 2 == 0:
a += 1
continue
print(a)
a += 1
else:
print(' End of loop ')
Use random randint method to write a small game to guess numbers
i = 1
while i < 10:
j = 1
while j <= i:
result = '%-3d'%(i*j)
print(f'{j} × {i}={result}', end='')
j += 1
print('\n')
i += 1
print('1 ~ 100 Guess the number game within integers, total 7 A chance! ')
import random
Min = 1
Max = 100
mynumber = random.randint(Min, Max)
i = 1
while i <= 7:
yournumber = int(input(' Please enter the number you guessed: '))
if yournumber == mynumber:
print(' Congratulations, you guessed it! You are so clever! ')
break
elif yournumber > mynumber:
Max = yournumber
print(f' Your guess is too big, in {Min}~{Max} Between oh! You still have {7-i} Chance! ')
i += 1
else:
Min = yournumber
print(f' Your guess is too small, in {Min}~{Max} Between oh! You still have {7-i} Chance! ')
i += 1
else:
print(' Opportunities have run out! It's a pity that you didn't guess right! ')