Python 渡轮票务程序
我需要制定一个关于订票的计划,我已经完成了座位安排的列表,有点像这样:Python 渡轮票务程序,python,python-3.x,variable-assignment,Python,Python 3.x,Variable Assignment,我需要制定一个关于订票的计划,我已经完成了座位安排的列表,有点像这样: def SC1000(): print(" ") print(" ") print("PURCHASING MODULE") print ("Your booking reference number is : ", bookingref) print("B - to purchase ticket for Business class") print("E - t
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,那将非常好
谢谢你的帮助
现在它没有将客户的详细信息保存到txt文件中
这是因为您正在编写一个空的customersdata
列表:
customersdata=[]
name =input("Please enter your full name : ")
cfile = open("sc1000.txt","a")
cfile.write("\n")
cfile.write(str(customersdata))
cfile.close()
是正确的;您当前正在将一个空列表写入文件,然后将项目追加到列表中。要解决此问题,请将项目附加到列表中,然后将其写入文件。以下是代码的固定代码部分:
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()
我得走了,打电话给我,明天(格林尼治标准时间)我会写我的答案。我还是有点迷茫哈哈,这是我第一次做python&我只是一名IT一年级的学生。您可以详细说明吗?
customersdata
一开始是一个空列表,您从不向其中添加任何内容。因此,当您将customersdata
写入文件时,您正在写入一个空列表。我假设你打算在列表中添加name
,也可能添加其他内容,但你从来没有这样做过。这是如何解决问题的CustomerData
仍然是一个空列表。哦,等一下,如果(sc==“A1”)打开(“sc1000.txt”,“r”):
是一个错误。也许你的意思是如果“A1”在open(“sc1000.txt”,“r”).read():
?@JohnGordon这是从OP的代码复制的。看看吧。