-
Notifications
You must be signed in to change notification settings - Fork 0
/
ch7 - loop.py
69 lines (53 loc) · 1.58 KB
/
ch7 - loop.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
65
66
67
68
69
# -*- coding: utf-8 -*-
'''
loop in python
for {var} in {List}:
code block
code block
code block
'''
for i in range(15):
print i
for i in range(-3,9):
print i
for i in range(10,2,-1):
print i
print range(1,31,2)
for year in range(1980,2020):
print "In the {}, there...".format(year)
catstype = ['manx','tabby','calico']
for cat in catstype:
print "That's a nice {} you have there!".format(cat)
numbers = [12,56,0,5,3]
for number in numbers:
if number == 0 :
print "Ugh, you give me a 0!"
continue #continue 用来跳过某些项
new_number = 100.0/number
print "100/{}={}".format(number,new_number)
cart = [1,89,154,9749,197,13,65]
for item in cart:
print item
if item>100:
print "you are going to require insurance on this order."
break
cart = [1,89,154,9749,197,13,65]
for item in cart:
print item
if item>10000:
print "you are going to require insurance on this order."
break
else:
print "There are no item over 10000"
# 只有为真时重复while
age = raw_input("Please give me your age in years(eg.30):")
while not age.isdigit():
print "I'm sorry, but {} isn't valid.".format(age)
age = age = raw_input("Please give me your age in years(eg.30):")
print "Thanks!your age is set to {}.".format(age)
# 无限循环
while True:
text = raw_input("Give me some text, and I'll count the e's. Enter 'q' to quit:")
if text == 'q':
break
print text.count('e')