循环使用一、while注意缩进。循环1whileTrue:wordinput(‘pleaseenteraword:‘)ifnotword:breakprint(‘Thewor 循环使用 一、while 注意缩进。 循环1 >>> while True: word = input(‘please enter a word:‘) if not word: br
循环使用
一、while 注意缩进。
循环1
>>> while True:
word = input(‘please enter a word:‘)
if not word:
break
print(‘The word was ‘+word)
结果
please enter a word:ddd
The word was ddd
please enter a word:ddd
The word was ddd
please enter a word:rrr
The word was rrr
循环2
word=‘‘
while not word:
word = input(‘name:‘)
print("hell, %s"%word)
结果
name:hhh
hell, hhh
二、for循环使用
1、列表输出
words = [‘a‘,‘b‘,‘c‘,‘d‘]
for word in words:
print(word, )
输出
a
b
c
d
2、遍历字典元素。
d = {‘a1‘: 33, ‘port6‘: 55, ‘a2‘: ‘ddd‘}
for key in d:
print("key:%s"%key),
print("value: %s"%d[key])
输出结果,注意顺序不是从字典左到右输出。随机的。
key:a2
value: ddd
key:a1
value: 33
key:port6
value: 55
注意和c语言一样跳出循环式break,继续下一次循环式continue。
python学习笔记--循环使用,布布扣,bubuko.com