我被要求拿出一个订票的程序,我已经完成了座位安排的列表,有点像这样:
def SC1000():
print(" ")
print(" ")
print("PURCHASING MODULE")
print ("Your booking reference number is : ", bookingref)
print("B - to purchase ticket for Business class")
print("E - to purchase ticket for Economy class")
print("M - to return to Main Menu")
bore=input("Enter your choice : ")
if (bore=="B"):
seatingchoice = [[" ", "A", "B", "C", "D", "E"],
["1" , "0" , "0" , "0" , "0" , "0"],
["2" , "0" , "0" , "0" , "0" , "0"]]
print("Seating Arrangement")
print("Business Class")
format_string="{:>4} {:>4} {:>4} {:>4} {:>4} {:>4}"
headers = seatingchoice [0]
header_row = format_string.format(*headers)
print(header_row)
print("-" * len(header_row))
for language in seatingchoice [1:3]:
print(format_string.format(*language))
sc=input("Enter your choice (eg:A3/a3): ")
if sc in open('sc1000.txt').read():
print("This seat has been taken, kindly choose another seat")
SC1000()
else:
customersdata=[]
name =input("Please enter your full name : ")
cfile = open("sc1000.txt","a")
cfile.write("\n")
cfile.write(str(customersdata))
cfile.close()
#for line in cfile:
if (sc=="A1") in open("sc1000.txt","r")():
seatingchoice[2][2]= 1
elif (sc=="A2") in open("sc1000.txt","r")():
seatingchoice[3][2]= 1
elif (sc=="B1") in open("sc1000.txt","r")():
seatingchoice[2][3]= 1
elif (sc=="B2") in open("sc1000.txt","r")():
seatingchoice[3][3]= 1
elif (sc=="C1") in open("sc1000.txt","r")():
seatingchoice[2][4]= 1
customersdata.append(bookingref)
customersdata.append(name)
customersdata.append(sc)
print("Boarding Ticket")
print("____________________________________")
print(" ")
print(" Date:",time.strftime("%d/%m/%Y"))
print(" Time:",time.strftime("%I:%M:%S"))
print(" Name : ",name)
print(" Ferry ID : Ferry 1")
print(" Boarding Time : 9.50am")
print(" Departure : Penang to Langkawi")
print(" Seating Class : Business Class")
print(" Seat Number : ",sc)
print(" Zone : A")
print(" Gate : B1")
print("_____________________________________")
print(" ")
print("Kindly print out the boarding pass as it will be needed at the gate.")
gmm=input("When done printing, press 'D' to go back to the Main Menu. ")
if (gmm=="D"):
mainmenu()
它可以工作,但我也需要存储数据时,用户键在他们想要的座位,现在它没有保存客户的详细信息到txt文件中,我确信我的代码有问题,但我不知道是什么。
例如,如果有人愿意与我分享如何在用户选择A1时将0改为1,那就太好了!
谢谢你的帮助
发布于 2018-06-27 00:42:45
@John Gordon's answer是正确的;您当前正在将一个空列表写入文件,然后将项目附加到该列表。要解决此问题,请将这些项附加到列表中,然后使用将其写入到文件中。以下是代码的固定代码部分:
else:
customersdata=[]
name =input("Please enter your full name : ")
if (sc=="A1") in open("sc1000.txt","r")():
seatingchoice[2][2]= 1
elif (sc=="A2") in open("sc1000.txt","r")():
seatingchoice[3][2]= 1
elif (sc=="B1") in open("sc1000.txt","r")():
seatingchoice[2][3]= 1
elif (sc=="B2") in open("sc1000.txt","r")():
seatingchoice[3][3]= 1
elif (sc=="C1") in open("sc1000.txt","r")():
seatingchoice[2][4]= 1
customersdata.append(bookingref)
customersdata.append(name)
customersdata.append(sc)
cfile = open("sc1000.txt","a")
cfile.write("\n")
cfile.write(str(customersdata))
cfile.close()
https://stackoverflow.com/questions/51013837
复制相似问题