会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133532个问题
JAVA 全系列/第一阶段:AI驱动的JAVA编程/飞机大战小项目训练 35791楼
JAVA 全系列/第五阶段:网页编程和设计/CSS3(旧) 35794楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 35797楼

老师好,关于三角形三个坐标求面积问题,代码如下:


import math
def get_info():  #输入三个坐标
    m = [[0, 0], [0, 0], [0, 0]]
    for i in range(3):
        m[i][0]=float(input('请输入第{0}个三角形的横坐标:'.format(i+1)))
        m[i][1]=float(input('请输入第{0}个三角形的纵坐标:'.format(i+1)))
    return m
def get_length(m):  #计算三边长
    a=[]
    b = math.sqrt((m[1][0] - m[2][0]) ** 2 + (m[1][1] - m[2][1]) ** 2)
    c = math.sqrt((m[2][0] - m[0][0]) ** 2 + (m[2][1] - m[0][1]) ** 2)
    d = math.sqrt((m[0][0] - m[1][0]) ** 2 + (m[0][1] - m[1][1]) ** 2)
    a.append(b)
    a.append(c)
    a.append(d)
    return a
def get_space(a): #判断并计算面积
    p=(a[0]+a[1]+a[2])/2
    s=math.sqrt(p*(p-a[0])*(p-a[1])*(p-a[2]))
    if s > 0:
        print('三角形面积是:',s)
    else:
        print('输入无效!')
m=get_info()
a=get_length(m)
get_space(a)


关于输入方面我有一点问题,目前的输入数据有点麻烦。我希望可以直接输入坐标然后计算,如:

输入 :(20,30),(40,50),(60,100)

然后计算。


但是如果输入带括号和逗号的数据,我不知道知道要如何处理,才能在程序中提取中对应的x,y值。

我想到的方法是:把‘(20,30),(40,50),(60,100)’字符串,通过切片的方式提取坐标数据,

但是有两个问题:

(1)只能取个位数,例如,只能分别取‘2’,'0' 不知道如何取出‘20’

(2)如果数据的位数变化,切片的位数也要相应变化,不然可能切到括号或者逗号。

所以想问一下这种情况如何处理,谢谢

Python 全系列/第一阶段:Python入门/函数和内存分析 35798楼
Python 全系列/第六阶段:数据库与AI协同技术实战/mysql的使用 35802楼

def dictionary_reader(filename):

    """
    :param filename: The type of the filename is string,
                    which means users need to input a string
                    type's filename.
    :return: a list which contain each word in the file
    """
    words_no_punctuation = []
    words_less_than_9 = []

    try:
        # open the file and execute the read operation
        f = open(filename, 'r')
        # append the each word speratly into a list
        words = f.readlines()
        for word in words:
            words_no_punctuation.append(word.strip())
        for word in words_no_punctuation:
            if len(word) <= 9:
                words_less_than_9.append(word)
        # print(len(words_less_than_9))


        # This is the test part of the dictionary_reader
        # Test passd, if length of list equal to 235887
        x = ''
        for word in words_less_than_9:
            if len(word) > 9:
                x = False
                break
            else:
                x = True
                continue

        if x == False:
            print("Not all of the len of word in this list, which is less than 9 characters.")
        else:
            print("-----Test passed! All words in this list are less than 9 characters-----")




        if len(words) == 235887:
            print("-----Test passed! There are 235887 words in the list!-----")
        else:
            print("The len of words is not equal to the len of word in the words.txt.")

        if isinstance(words_less_than_9, list):
            print("-----Test pssed! The function dictionary_reader will return the all words which len are less than 9 characters in a list!-----")
        else:
            print("The return stuff is not the list type.")
        return words_less_than_9
    except BaseException as e:
        # print the error information when programme meet error
        # it is easy to help users matain the programme
        print(e)
    finally:
        # make sure the file stream will be closed in any suitation of programme
        f.close()
        
        
def word_lookup():
    # read file at first
    # words is a list, and each elements in the words list is a word.
    words = dictionary_reader('words.txt')
    sorted_dictionary = []
    longest_words = []
    possibility_words = []
    for word in words:
        """  
            every single word in the words list will be sorted by alphabetical.
        """
        word = word.strip()
        # ensure every single word in the list is lowercase
        sorted_dictionary.append("".join(sorted(word)).lower())
    print(sorted_dictionary)
words.txt
WechatIMG945.png

老师您好,这是我写的的两个函数。在word_lookup里面我尝试把文件里面的每一个单子都变成字母排序,但是控制台输出的第一个单词并不是按字母排序去排序的,请问一下这是为什么?

输出在附件里面传上去了,不知道为什么不能够上传图片,抱歉。

谢谢老师

Python 全系列/第四阶段:函数式编程和核心特性/函数式编程 35803楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637