发布时间:2019-06-24 15:53:34编辑:auto阅读(2003)
1,判断条件(例如保证输入)
- import math
- #Get base
- inputOK = False
- while not inputOK:
- base = input('Enter base: ')
- if type(base) == type(1.0): inputOK = True
- else: print('Error. Base must be floating point number.')
- #Get Height
- inputOK = False
- while not inputOK:
- height = input('Enter height: ')
- if type(height) == type(1.0): inputOK = True
- else: print('Error. Height must be floating point number.')
- hyp = math.sqrt(base*base + height*height)
- print 'Base: '+str(base)+',height: '+str(height)+', hyp: '+ str(hyp)
注意inputOK,很好读的代码吧?是MIT计算机科学及编程导论上的作业。抽时间去网易公开课去看看。
2,达到条件才输出
- def calcE():
- x = y = 1
- sum = 0
- while (sum < 4000000):
- sum += (x + y)
- x, y = x + 2 * y, 2 * x + 3 * y
- return sum
这个没什么好说的、很清晰。
上一篇: Centos5.5升级Python
下一篇: python基础总结--随笔
50548
49866
40458
37449
31880
28761
27679
22460
22457
20769
554°
1176°
959°
885°
1143°
1002°
1626°
3012°
2731°
2058°