Python文件I / O和列表问题

时间:2015-04-16 00:50:26

标签: python list io

所以我必须使用列表和输入和输出文件进行Python编程分配,我被困住了。我必须查找具有名称,颜色和周长的三角形。三角形位于.txt文件中,如下所示:

Triangle1,red,0,0,0,1,1,0
Triangle2,blue,1,0,0,1,0,0
GeorgeJetson,BLUE,20.1,10.5,23,5,6,33.3
Triangle4,BlUe,10,10,20,20,0,0
Triangle2,red,1,1,0,1,0,0
Gusty,orange,0,0,0,2,2,2
Gusty,red,0,0,0,2,2,2
triangle2,orange,0,0,0,2,2,2

到目前为止,我的代码是:

from math import sqrt

def peri(x1,y1,x2,y2,x3,y3):
    side1 = sqrt(((x1-x2)**2)+((y1-y2)**2))
    side2 = sqrt(((x2-x3)**2)+((y2-y3)**2))
    side3 = sqrt(((x1-x3)**2)+((y1-y3)**2))
    p = side1 + side2 + side3
    return p

def main():
    # Handshake
    print("This program performs database processing.")
    # Prompt for input and output file names
    infileName = input("Enter input file name: ")
    outfileName = input("Enter output file name: ")

    # Open input file name and read it
    infile = open(infileName,"r")

    # Read file, create triangle list
    trilist = []
    for line in infile:
        name,color,x1,y1,x2,y2,x3,y3 = line.split(",")
        x1 = eval(x1)
        y1 = eval(y1)
        x2 = eval(x2)
        y2 = eval(y2)
        x3 = eval(x3)
        y3 = eval(y3)
        perimeter = peri(x1,y1,x2,y2,x3,y3)
        triangle = name,color,perimeter
        trilist.append(triangle)

    # Close input file
    infile.close()

    LowerCaseOutfile = False
    AscendingOutfile = False
    print("")
    print("Commands:")
    print("perimeterbyname: Display triangle perimenters by name")
    print("perimeterbycolor: Display triangle perimenters by color")
    print("namebycolor: Display triangle names by color")
    print("colorbyname: Display triangle colors by name")
    print("lowercase: Display triangle names and colors in lowercase")
    print("ascending: Display triangle perimeters in ascending order")
    print("quit: Quit the program")
    print("")
    userInput = input("Enter a command: ")
    while userInput != "quit":
        if userInput == "perimeterbyname":
            triname = input("Enter a triangle name: ")
            for triangle in trilist:
                if triname == name:
                    print(triangle)
                else:
                    print("Invalid name")

        print("")
        print("Commands:")
        print("perimeterbyname: Display triangle perimenters by name")
        print("perimeterbycolor: Display triangle perimenters by color")
        print("namebycolor: Display triangle names by color")
        print("colorbyname: Display triangle colors by name")
        print("lowercase: Display triangle names and colors in lowercase")
        print("ascending: Display triangle perimeters in lascending order")
        print("quit: Quit the program")
        print("")
        userInput = input("Enter a command: ")


    # Open output file name and write it
    outfile = open(outfileName, "w")

    # Write to the output file
    print(trilist, file = outfile)

    # Close output file
    outfile.close()

    print("Triangles have been written to", outfileName)


main()

我无法从if语句的列表中获取三角形的名称。 Python无法识别变量“name”。帮助

1 个答案:

答案 0 :(得分:1)

我猜你正在谈论在循环中打印出一个三角形,只要它与用户输入匹配。 “name”无效的原因是python不存储在创建三角形列表时使用的变量,只存储它们的值。因此,当你再次阅读它们时,它只是一个价值。

因此,每个trilist(这是一个元组列表)是: [(“名称”,“颜色”,周长),(“名称”,“颜色”,周长)]其中“名称”,“颜色”和周长是每个三角形的实际值。

现在你可以将元组的值拉出到单独的变量中:

name, color, perimeter = triangle
在你的for循环中

然后按预期使用变量或直接手动索引元组:

name = triangle[0]

现在,为了实现这一点,您实际上可以通过两种方式编写for循环:

        # leave triangle as a tuple
        for triangle in trilist:
            if triname == triangle[0]:
                print(triangle)
            else:
                print("Invalid name")

或:

        # assign elements of tuple to separate variables
        for name,color,perimeter in trilist:
            if triname == name:
                print(triangle)
            else:
                print("Invalid name")