2016-05-31 78 views
0
import csv 

samsung = ['samsung','s1','s2','s3','s4','s5','s6','s7','galaxy'] 
iphone = ['iphone'] 
problemTypes = [] 
solution = [] 
instruction = [] 

def solve(): 
    def foundProblem(): 
     for queryPart in whatProblem: 
      for problem in problemTypes: 
       if queryPart == problem: 
        return True 
    solved = False 
    readCSV = csv.reader(csvfile, delimiter = ',') 
    for row in readCSV: 
     solution = row[0] 
     problemTypes = row[1].split() 
     instruction = row[2] 
     if foundProblem(): 
      print('The solution is to '+solution) 
      print(instruction) 
      solved = True 
    if solved == False: 
     print('Solution not found.\nPlease contact your supplier') 

whatProblem = str(input('What seems to be the issue with your smartphone?\n')).lower().split() 
version = input('What type of phone do you have?\n').lower().split() 
if version == iphone: 
    with open('iPhone.csv') as csvfile: 
     solve() 
elif version in samsung: 
    with open('samsung.csv') as csvfile: 
     solve() 
else: 
    print('Phone not supported') 

這是嘗試使用多個csv文件創建故障處理程序,但遇到了三星部件的問題。它似乎沒有注意到輸入實際上是三星變量的一部分。我在這裏是新的,所以如果我已經格式化了這個錯誤,請通知我,如果解決方案非常簡單,請知道我是編碼新手。如何檢查變量中的變量值是否保存在另一個變量中

+0

究竟你需要幫助的:? – Jeremy

回答

0

至少嘗試改變這一行:

version = input('What type of phone do you have?\n').lower().split() 

到:

version = input('What type of phone do you have?\n').lower().split()[0] 

但是讀取輸入您目前強制用戶輸入「三星」,這是不是最容易的辦法。繼續學習和嘗試,它會工作得很好!

0

這是哪個位您遇到的問題,但這種提取物看起來錯並不完全清楚:

with open('iPhone.csv') as csvfile: 
    solve() 

你可能打算用塊內csvfile

with open('iPhone.csv') as csvfile: 
    solve(csvfile) 

,並將solve的實現更改爲接受csvfile作爲參數。事實上,它看起來像是在嘗試(和失敗)通過全局變量進行通信;即使這樣做確實有效,但這種做法很糟糕,導致無法維護的代碼!

相關問題