2017-04-21 7 views
0
に実行されている

私のPythonプログラムはinputs.Theメインコードの束を持っているテキストファイルを読み込み、そのは、なぜ私のプログラムではwhileループは無限大

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() 
関連する問題