5-8Geometry
import math
def sqcube():
s = float(raw_input(‘enter length of one side: ‘))
print ‘the area is:‘, s ** 2., ‘(units squared)‘
print ‘the volume is:‘, s ** 3., ‘(cubic units)‘
def cirsph():
r = float(raw_input(‘enter length of radius: ‘))
print ‘the area is:‘, math.pi * (r ** 2.),‘(units squared)‘
print ‘the volume is:‘, (4. / 3.) * math.pi * (r **3.), ‘(cubic units)‘
5-11Modules
(a) 使用循环和算术运算,求出 0-20 之间的所有偶数
for i in range(0,22,2):
print i
or
for i in range(22):
if i%2 ==0:
print i
(b) 同上,不过这次输出所有的奇数
for i in range(1,20,2):
print i
or
for i in range(20):
if i%2 !=0:
print i
时间: 2024-10-01 20:20:34