-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol flow.py
64 lines (50 loc) · 1.33 KB
/
control flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#if
number = 23
guess = int(input('Enter a number:')) #input函数将以字符串的形式返回我们输入的内容
if number == guess:
print('you guess it')
elif guess < number:
print('it is higher than you guess')
else:
print('it is smaller than you guess')
print('Done')
#while
number_1 = 23
running = True
while running:
guess_1 = int(input('Enter a number:'))
if guess_1 == number_1:
print('you guess it')
running = False
elif guess_1 < number_1:
print('it is higher than you guess')
else:
print('it is smaller than you guess')
else:
print('the while loop end in this')
print('Done')
#for
for i in range(5):
print(i)
else:
print('the for loop is over')
# range(1,5) 输出[1,2,3,4]
# range(1,5,2) 输出[1,3]
# range()每次只会返回一个数字,如果需要得到完整的数字列表,需要使用list()
# list(range(1,5)) [1,2,3,4]
#break 终止循环,不再执行循环
while True:
s = input('Enter something:')
if s == 'quit':
break
print('Length for something is',len(s))
print('Done')
#continue 跳出当前循环,执行下一次循环
while True:
s = input('Enter something:')
if s == 'quit':
break
if len(s) < 3:
print('Too small')
continue
print('input is of sufficient length')