首页 > 代码库 > Beginning Python From Novice to Professional (5) - 条件与循环
Beginning Python From Novice to Professional (5) - 条件与循环
条件与循环
条件执行:
name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumby'
What is your name? Gumby Hello, Mr.Gumby
name = raw_input('What is your name? ') if name.endswith('Gumby'): print 'Hello, Mr.Gumby' else: print 'Hello, stranger'多个条件:
num = input('Enter a number: ') if num > 0: print 'The number is positive' elif num < 0: print 'The number is negative' else: print 'The number is zero'
Enter a number: 5 The number is positive
Enter a number: -1 The number is negative
Enter a number: 0 The number is zerowhile循环:
x = 1 while x<=100: print x x+=1for循环:
numbers = [0,1,2,3,4,5,6,7,8,9] for number in numbers: print number
0 1 2 3 4 5 6 7 8 9循环中的else:
for n in range(99,81,-1): root = sqrt(n) if root == int(root): print n break else: #只在没有调用break时执行 print "Didn't find it!"
Beginning Python From Novice to Professional (5) - 条件与循环
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。