我的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
您没有从文件中读取的输入中剥离换行符。每行的末尾都有一个隐藏的
\n
。只需添加.strip()
就可以解决这个问题相关问题 更多 >
编程相关推荐