2017-04-21 1 views
0
에 실행

내 파이썬 프로그램은 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() 
관련 문제