我的代码在未缩进且不在python中的def函数中时工作
当我的代码不在def函数中且没有缩进时,我有一段代码可以正确运行。这是我缩进时的代码我的代码在未缩进且不在python中的def函数中时工作,python,python-3.x,python-3.5,Python,Python 3.x,Python 3.5,当我的代码不在def函数中且没有缩进时,我有一段代码可以正确运行。这是我缩进时的代码 import csv my_file = open("marqueeTables.csv", "r") search_data = input("Please enter the data you are looking for") search_data = (search_data + " metre wide") #print(search_data) reader = csv.reader(my_fi
import csv
my_file = open("marqueeTables.csv", "r")
search_data = input("Please enter the data you are looking for")
search_data = (search_data + " metre wide")
#print(search_data)
reader = csv.reader(my_file)
for row in my_file:
if search_data in str(row): # == "6 metre wide":
stripedRow = row.strip()
splitStrippedRow = stripedRow.split(",")[0]
print(splitStrippedRow)
#print(row)
It prints "6 metre wide" or "12 metre wide" depending on whether I type 6 or 12.
下面是类似的代码,但在def中只更改了一些内容:
def userInfo():
while True:
w = str(input("What size width marque would you like 6 or 12: "))
if w == "6":
myFile = open("userDetails.csv", "a+")
myFile.write(str(w) + ", ")
myFile.close()
break
elif w == "12":
myFile = open("userDetails.csv", "a+")
myFile.write(str(w) + ", ")
myFile.close()
break
else:
print("Pleas type 6 or 12")
w = (w + "metre wide")
my_file = open("marqueeTables.csv", "r")
#search_data = input("Please enter the data you are looking for")
reader = csv.reader(my_file)
for row in my_file:
if w in str(row):
stripedRow = row.strip()
splitStrippedRow = stripedRow.split(",")[0]
print(splitStrippedRow)
userInfo()
当我运行代码时,它会再次打印6米宽和座位数,12米宽和座位数,因为它在表中有两次
有人能告诉我为什么我的代码不能用python工作吗?谢谢,那太好了。我正在使用python 3.5。谢谢在第二个代码示例中,您定义了userInfo,但从未调用过它,因此它从未运行过。什么意思是不起作用?请阅读。在您的第二个代码块中,您没有调用函数userInfo…在您的第二个代码块中,elif w==12:应该是未插入的。是的,它在我的代码中未插入,但当我复制和粘贴代码时,缩进错误,因此我必须再次执行,因此我在@agoldI have中出错,但它仍然没有运行properly@rodude123:我明白了第二个代码段中没有函数调用。提示:它看起来像userInfo。即使在我调用了这个函数之后,它也给出了我想要的错误输出。还有其他解决方案吗