圆的面积、求平方、判断字符串对称性

2019-04-24  本文已影响0人  以我丶之姓

圆的面积

import math
while True:
    r = str(input('please enter the radius of the circle:'))
    if not r.isalpha():
        r = float(r)
        circleArea = math.pi * r**2
        print "圆的面积是:{}".format(circleArea)
        break
    else:
        print "格式有误,请重新输入"
        continue

求平方

def square(start, end):
    result = []
    for num in range(start, end+1):
        if num % 2 == 0:
            result.append(num**2)
    print result

square(1,10)

判断输入的字符串是否对称

def func():
    while True:
        print "继续请按C!,取消请按Q!"
        inOrOut = raw_input()
        if inOrOut == "C" or inOrOut == "c":
            symmetricsString()
        elif inOrOut == 'q' or inOrOut == 'Q':
            print 'bye!'
            exit()
        else:
            print '输入错误,请重新输入!'
            print ' '
        continue


def symmetricsString():
    input_str = raw_input('请输入一个字符串:')
    if len(input_str) != (0 or 1):
        tangentPoint = len(input_str) // 2
        left = input_str[:tangentPoint]
        print left
        right = input_str[:tangentPoint-1:-1]
        print right
        if left == right:
            print '对称'
            func()
        else:
            print '不对称'
            func()
    else:
        print "输入不能为空或一个字符!"
        func()
symmetricsString()

求一个数可以有哪两个数的平方和相加组成

def func():
    n = int(input('please enter your number:'))
    result = []
    for i in range(int(math.sqrt(n/2))+1):
        j = math.sqrt(n-i*i)
        if j % 1 == 0:
            result.append((i, int(j)))
    if len(result) == 0:
        print 'NO'
    else:
        for i, j in result:
            print i, j
func()
上一篇 下一篇

猜你喜欢

热点阅读