我需要提出一个预订机票的程序,我已经完成了座位安排清单,有点像这样:
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文件中,并且我确定我的代码有问题,但是我不知道是什么。
ALSO ,例如,如果有人想与我分享如何在用户选择A1时将0更改为1,那就太好了!
谢谢您的帮助
答案 0 :(得分:1)
现在不会将客户详细信息保存到txt文件中
那是因为您要写一个空的customersdata
列表:
customersdata=[]
name =input("Please enter your full name : ")
cfile = open("sc1000.txt","a")
cfile.write("\n")
cfile.write(str(customersdata))
cfile.close()
答案 1 :(得分:0)
@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()