将多边形内的角度(和边)数量与所有可能结果的概念联系起来

时间:2017-07-08 16:32:01

标签: python python-3.x probability angle

我试图在给定的形状中找到所有可能的角度和边。但是,我在挣扎。这是我的代码的开始。

SidesNumber = int(input("How many sides does your shape have: "))
SidesNumber = str(SidesNumber)
ShapeName = input("What is your shape's name. Please enter "+ SidesNumber +" letters: ")
SidesNumber = int(SidesNumber)
while (len(ShapeName) != SidesNumber) or(not ShapeName.isalpha()):
    if (len(ShapeName) != SidesNumber) and(not ShapeName.isalpha()):
        print("Your input does not match the number of vertices your shape has and does not consist of only letters!") 
        SidesNumber = str(SidesNumber)
        ShapeName = input("What is your shape's name. Please enter "+ SidesNumber +" letters: ")
        SidesNumber = int(SidesNumber)
    elif len(ShapeName) != SidesNumber:
        print("Your input does not match the number of vertices your shape has!")
        SidesNumber = str(SidesNumber)
        ShapeName = input("What is your shape's name. Please enter "+ SidesNumber +" letters: ")
        SidesNumber = int(SidesNumber)
    elif not ShapeName.isalpha():
        print("Your input does not consist of letters only!")
        SidesNumber = str(SidesNumber)
        ShapeName = input("What is your shape's name. Please enter "+ SidesNumber +" letters: ")
        SidesNumber = int(SidesNumber)

现在我想做的是让计算机根据用户的输入计算所有不同的角度和边。 因此,如果他们为ShapeName输入SidesNumber和ABC,我希望计算机能够找到所有角度和边或者所有可能的结果。所以角度应该是∠ABC,∠ACB,∠BAC。双方应该是AB,BC,AC。

此外,我希望将这些结果保存在不同的变量中,例如side1 = 'AB'side2 ='BC'以及side3 = 'AC'。适用于类似angle1 = '∠ABC'angle1 = '∠ACB'以及angle1 = '∠BAC'

的角度

我已经有了这段代码:

import itertools
dataset = ['1','2','3A','4']
var = list(itertools.combinations(dataset, 3))
print(var)

但对我来说似乎毫无用处。

1 个答案:

答案 0 :(得分:0)

我不确定我是否完全按照您对您尝试做的描述,但认为这些产生结果的示例可能会有所帮助:

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = [iter(iterable) for _ in range(2)]
    next(b, None)
    return zip(a, b)

# hardcode a name for non-interactive testing
shape_name = 'ABC'

sides = [''.join(pair) for pair in pairwise(shape_name + shape_name[0])]
print('sides: {}'.format(sides))

def angle_name(shape_name, side):
    this = shape_name.index(side[-1])  # index of letter for end of this segment
    try:
        nx = shape_name[this+1]  # get letter for next segment
    except IndexError:
        nx = shape_name[0]  # wrap around

    return '∠'+side+nx

angles = [angle_name(shape_name, side) for side in sides]
print('angles: {}'.format(angles))

输出:

sides: ['AB', 'BC', 'CA']
angles: ['∠ABC', '∠BCA', '∠CAB']

无论如何,我强烈建议您阅读并开始关注PEP 8 - Style Guide for Python Code,尤其是Naming Conventions部分。