My python program is supposed to be a mock craigslist forum post that reads a text file that has a bunch of inputs.The main code looks like that
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)
and the inputs are
1
b
50
1
m
30
3
2
m
40
3
4
You aren't striping the newline characters from the inputs you are reading from the file. Each of the lines has a hidden \\n
at the end of them. It is a simple fix to just add .strip()
choice = myFile.readline().strip()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.