think python学习心得-(4)有返回函数

练习题

6-1

def compare(x,y):
    if x>y:
        return 1
    elif x==y:
        return 0
    else:
        return -1

a=[int(i) for i in raw_input('please input 2 number: ').split()]
print compare(a[0],a[1])

这里使用

a=[int(i) for i in raw_input('please input 2 number: ').split()]
语句可以实现用空格隔开的多个数据的输入,就像matlab中的向量一样,很好用!

6-2

import math
def hypotenuse(x,y):
    return math.sqrt(x**2+y**2)
    
a=[int(i) for i in raw_input('please input 2 number: ').split()]

print hypotenuse(a[0],a[1])

6-3

def is_between(x,y,z):
    if x<=y:
        if y<=z:
            return True
        else:
            return False
    else:
        return False

a=[int(i) for i in raw_input('please input 3 number: ').split()]
print is_between(a[0],a[1],a[2])

6-4

返回值是int类型的90

6-7

def is_power(a,b):
    if a%b==0:
        a=a/b
        if a>b:
            is_power(a,b)
        elif a==b:
            print 'yes'
        else:
            print 'no'
    else:
        print 'no'
print 'Please number a and b'
x=int(raw_input())
y=int(raw_input())
is_power(x,y)

6-8

flag=[]
def gcd(a,b):
    m=min(a,b)
    for i in range(m):
        if a%(i+1)==0:
            if b%(i+1)==0:
                flag.insert=(i,i)
print 'please input a and b to get GCD'
x=int(raw_input())
y=int(raw_input())
gcd(x,y)
print flag

此处涉及到了list操作


你可能感兴趣的:(think python学习心得-(4)有返回函数)