【发布时间】:2020-11-11 13:19:23
【问题描述】:
我正在尝试创建一个程序,将酒店房间号分配给客人。分配后,在字典中,键的值应更新为“不可用”或“保留”。但是,问题是,在我关闭程序之后。那些已更新的房间号将重新变为“可用”。
有没有办法改变这个值,即使在我关闭程序后它也会保持更新?
import math
f = open('room.txt', 'r+')
#Hotel Rooms
for i in range(1, 7):
locals()['Level{}'.format(i)] = {} # Creating Dictionary
start = 101
for x in range(1, 7):
for i in range(start, start + 10):
eval("Level" + str(x))[i] = 'Available' # Adding Values into Dictionary
start += 100
# heading function
def heading(number, header):
for i in range(60):
print('-', end = '')
print()
for i in range(number):
print('*', end = ' ')
print('\n' + header)
for i in range(number):
print('*', end = ' ')
print()
# customer details function
def customer():
print('''Room Types:
1 --> Single (Level 1)
2 --> Double (Level 2)
3 --> Triple (Level 3)
4 --> Quad (Level 4)
5 --> Queen (Level 5)
6 --> King (Level 6)
''')
room = input('Room Type: ')
name = input('Name: ')
hp = input('Mobile Number: ')
guests = input('Number of Guests: ')
arrival = input('Arrival Date(dd/mm/yyyy): ')
departure = input('Departure Date(dd/mm/yyyy): ')
confirm = input('Confirmation(Y/N): ')
conf(confirm, room, option)
# confirmation function + room for guest
def conf(con, num, opt):
if con == 'Y':
try:
for level, availability in eval('Level' + str(num)).items():
if availability == 'Available':
print('Guest Room Number:', level)
if option == '1':
eval('Level' + str(num))[level] = 'Reserved'
elif option == '2':
eval('Level' + str(num))[level] = 'Unavailable'
print('\nRoom Recorded!')
break
for i in range(1, 7):
f.write('Level ' + str(i) + '\n')
for key in eval('Level' + str(i)):
f.write(str(key) + ": " + eval('Level' + str(i))[key] + '\n')
f.write('\n')
f.close()
except NameError:
print('\nInvalid Room Type.')
elif con == 'N':
print('\nRecord Not Recorded.')
else:
print('\nInvalid Confirmation.')
# sub-headings (options)
def options():
print('''
Choose one of the following options (0-3):
0 <-- Back to Main Menu.
1 --> Booking Engine
2 --> Check In
3 --> Check Out''')
while True:
cus = '* Customer Registration *'
num = math.ceil(len(cus)/2)
heading(num, cus)
options()
option = input('Enter your option: ')
# Booking Engine
if option == '1':
boo = '* Booking Engine *'
num = math.ceil(len(boo)/2)
heading(num, boo)
customer()
#Check In
elif option == '2':
chein = '* Check In *'
num = math.ceil(len(chein)/2)
heading(num, chein)
customer()
else:
print("\nInvalid Option.")
continue
【问题讨论】:
-
您需要在某处保留更改。程序分配在程序运行时使用的内存,并在程序关闭时清除,因此您需要以某种方式持久更改。如果此程序仅用于学习目的,您可以使用文件。
-
看模块
pickle -
@Capie 有没有使用文件使程序运行的代码示例?
标签: python dictionary key-value-store