python 5 条件判断和循环

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xingkongyingke/article/details/45424771
#!/usr/bin/env python
# -*- coding: utf-8 -*-
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 -2
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-8

:

IndentationError: unindent does not match any outer indentation level

遇到这个错误是告诉我们缩进不对

print u'------while---------------'
sum = 0
n = 3
while n>0:
sum = sum + n
n = n -2
sum = sum - 1  
 #容易出现缩进错误,Tab和空格的不同,解决办法是sum = sum - 1移动到上一行,回车让Editplus帮我们换行
print sum
这里有趣的事sum = sum - 1位置的不同(前面是否有Tab,决定了sum = sum - 1 语句是否属于while{}中


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页