Python 文件 I/O 和列表有问题
Trouble with Python file I/O and lists
所以我必须用列表和输入输出文件做一个 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"。帮忙?
我猜您是在谈论 while 在循环中仅当它与用户输入匹配时才打印出三角形。 "name" 无效的原因是 python 不存储您在创建三角形列表时使用的变量,仅存储它们的值。因此,当您再次读取它们时,它只是一个值。
因此,每个三元组(元组列表)是:
[("name","color",perimeter),("name","color",perimeter)] 其中 "name", "color" 和 perimeter 是实际的每个三角形的值。
现在您可以将元组的值提取到单独的变量中:
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")
所以我必须用列表和输入输出文件做一个 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"。帮忙?
我猜您是在谈论 while 在循环中仅当它与用户输入匹配时才打印出三角形。 "name" 无效的原因是 python 不存储您在创建三角形列表时使用的变量,仅存储它们的值。因此,当您再次读取它们时,它只是一个值。
因此,每个三元组(元组列表)是: [("name","color",perimeter),("name","color",perimeter)] 其中 "name", "color" 和 perimeter 是实际的每个三角形的值。
现在您可以将元组的值提取到单独的变量中:
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")