python 5 条件判断和循环
来源:程序员人生 发布时间:2015-05-19 07:51:57 阅读次数:2234次
#!/usr/bin/env python
# -*- coding: utf⑻ -*-
age = 3
if age >= 18:
print 'adult'
elif age >= 6:
print 'teenager'
else:
print 'kid'
print '----------------------'
names = ['jie', 'bob', 'trcy']
for name in names:
print name
print '------------------------'
sum = 0
for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
sum = sum + x
print sum
print u'-----------range(整数生成)----------------'
sum = 0
for x in range(101):
sum = sum + x
print sum
print u'------while---------------'
sum = 0
n = 3
while n>0:
sum = sum + n
n = n ⑵
sum = sum - 1
print sum
print u'---------输入raw_input---------------'
birth = int(raw_input('birth:')) #注意raw_input输入的数据都是字符
if birth<20:
print u'00前'
else:
print u'00后'
注意代码保存的格式是utf⑻
:
IndentationError: unindent does not match any outer indentation level
遇到这个毛病是告知我们缩进不对
print u'------while---------------'
sum = 0
n = 3
while n>0:
sum = sum + n
n = n ⑵
sum = sum - 1 #容易出现缩进毛病,Tab和空格的不同,解决办法是sum = sum - 1移动到上1行,回车让Editplus帮我们换行
print sum
这里有趣的事sum = sum - 1位置的不同(前面是不是有Tab,决定了sum = sum - 1 语句是不是属于while{}中
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠