2015-11-23 66 views
-3

一个高清功能我有一段代码运行正常,当我我的代码是不是在高清功能,而不是缩进我的代码工作。缩进时,这是我的代码。时不缩进,而不是在蟒蛇

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 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.米宽的”和“座位数”和“十二米宽的”和“座位”了(因为它是表中的两倍。)

有人能告诉我为什么我的代码是不是在蟒蛇感谢工作将是巨大的。我正在使用python 3.5。由于

+2

你是什么意思“不起作用”?请阅读[mcve]。 –

+0

在你的第二个代码块中,你不要调用函数'userInfo' ... – AlG

+0

同样在你的第二个代码块中'elif w ==“12”:'应该是缩进的。 – agold

回答

1

在第二个代码示例,您可以定义userInfo,但你永远不调用它,所以它永远不会运行。

+0

我有,但它仍不能正常运行 – rodude123

+0

@ rodude123:我在第二个片段中看不到函数调用。 (提示:它看起来像'userInfo()'。) – jwodder

+0

即使在我调用该函数后,它会给出错误的输出,我想要另一个解决方案 – rodude123