2017-04-21 48 views
0

我的Python程序應該是一個模仿Craigslist網站論壇的帖子,上面寫着有一堆inputs.The主代碼的文本文件看起來像爲什麼我在程序的同時運行循環到無窮遠

filename = input() 
myFile = open(filename, "r") 
mssgBoard = [["bicycle"], ["microwave"], ["dresser"], ["truck"],["chicken"]] 
choice = "" 
while (choice != "4"): 
    print ("1. Insert an item.") 
    print ("2. Search an item.") 
    print ("3. Print the message board.") 
    print ("4. Quit.") 
    choice = myFile.readline() 
    if (choice == "1"): 
     userInput = "" 
     userInput1 = 0 
     print ("Enter the item type-b,m,d,t,c: ") 
     userInput = myFile.readline() 
     print ("Enter the item cost: ") 
     userInput1 = myFile.readline() 
     if userInput == "b": 
      mssgBoard[0].append(userInput1) 
     elif userInput == "m": 
      mssgBoard[1].append(userInput1) 
     elif userInput == "d": 
      mssgBoard[2].append(userInput1) 
     elif userInput == "t": 
      mssgBoard[3].append(userInput1) 
     elif userInput == "c": 
      mssgBoard[4].append(userInput1) 

和輸入是

1 
b 
50 
1 
m 
30 
3 
2 
m 
40 
3 
4 

回答

0

您不是從您正在從文件中讀取的輸入中分割換行符。每一行在其末尾都有一個隱藏的\n。這是一個簡單的修復,只需添加.strip()

choice = myFile.readline().strip() 
相關問題