一、求算術平方根 a=0 x=int(raw_input('Enter a number:')) if x >= 0: while a*a < x: a = a + 1 if a*a != x: print x,'is not a perfect square' else: print a else
一、求算術平方根
a=0 x=int(raw_input('Enter a number:')) if x >= 0: while a*a < x: a = a + 1 if a*a != x: print x,'is not a perfect square' else: print a else: print x,'is a negative number'
二、求約數
方法一:
divisor = [ ] x=int(raw_input('Enter a number:')) i=1 while i<=x: if x%i ==0: divisor.append(i) i = i +1 print 'divisor:',divisor
方法二:
divisor = [ ] x=int(raw_input('Enter a number:')) for i in range(1,x+1): if x%i ==0: divisor.append(i) # 此行也可以換成 divisor = divisor + [i] print 'divisor:',divisor