while usage
The while condition satisfies:
Conditional Execution Statements
else:
Statements executed without satisfying conditions
Such as:
i=0 sum=0 while i<=100: sum +=i i+=1 print(sum)
while dead cycle
while True:
print('~~~~~~~~~~~~~')
while 2>1:
print('%%%%%')
while nested loop
n = int(input('Please enter the number of lines you want to print.: ')) i = 0 while i < n: j = 0 while j <= i: print('*', end=''); j = j + 1; print('') i = i + 1 //perhaps for i in range(1,10): for j in range(1,i+1): print('*',end='') print('') row = 1 while row <= 9: col = 9 while col >= row: print('*', end='') col -= 1 print('') row += 1 row = 1 while row <= 9: kongge = 1 while kongge <= 9 - row: print(' ', end='') kongge += 1 col = 1 while col <= row: print('*', end='') col += 1 print('') row = row + 1 n=int(input('Please enter the number of lines you want to print.: ')) row = 1 while row <= n: kongge = 1 while kongge <= row - 1: print(' ', end='') kongge += 1 col = 1 while col <= n - row +1: print('*', end='') col += 1 print('') row += 1
#\ t: Output a tab in the console to help us align vertically when we output text
print('1 2 3')
print('10 20 30')
print('1\t2\t3')
print('10\t20\t30')
#\ n: Output a newline character in the console
print('hello\npython')
# Escape characters
print('what's')
print("what's")
Practice;
Number guessing game
if , while, break
1. The system generates a number of 1-100 at random.
** How to generate integers randomly, import module random, and execute random.randint(1,100);
2. Users have five chances to guess numbers.
3. If the number guessed by the user is larger than the number given by the system, print "too big".
4. If the number guessed by the user is less than the number given by the system, print "too small";
5. If the number guessed by the user is equal to the number given by the system, print "Congratulations on winning 1 million" and exit the cycle.
import random computer=random.randint(1,100) print(computer) i=0 while i<5: num = int(input('Please enter the number you want to guess.: ')) if num>computer: print('too big') print('You still have the rest.%d Second chance' % (4 - i)) elif num<computer: print('too small') print('You still have the rest.%d Second chance' % (4 - i)) else: print('Congratulations on winning the prize') break i=i+1 else: print('Log in more than five times, please wait for 100 s After login')