我的python程序应该是一个模拟craigslist论坛帖子,它读取一个包含大量输入的文本文件。主要代码看起来像那样
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 :(得分:0)
您没有从正在从文件中读取的输入中去除换行符。每一行在其末尾都有一个隐藏的\n
。只需添加.strip()
choice = myFile.readline().strip()