在Python中,这不能循环很多次,也不能在def中找到ID号
每次我试着运行这个程序时都会出错
"NameError: name 'askname' is not defined"
在textfile中只保留最新的数据
#def new_booking
的输出是连续地将数据保存在文件中,但实际的输出是将数据保存在文件中,只有一句话"NameError: name 'askname' is not defined"
import random
# total=0
# people=0
total1 = 0
total2 = 0
# mini bar
def mini_bar():
mini_bar_total = 0
print("£50 for the Bar")
askbar = input("Would you like a mini bar? Y/N")
if askbar.upper() == "Y":
mini_bar_total = mini_bar_total + 50
return mini_bar_total
# breakfast
def breakfast(people, asknights):
breakfast_total = 0
print("£25 for breakfast")
askdinner = input("Would you like dinner? Y/N")
if askdinner.upper() == "Y":
breakfast_total = (people * 25) * asknights
print("total: £", breakfast_total)
return breakfast_total
# dinner
def dinner(people, asknights):
dinner_total = 0
print("£25 for Dinner")
askdinner = input("Would you like dinner? Y/N")
if askdinner.upper() == "Y":
dinner_total = (people * 25) * asknights
return dinner_total
# number customers
def num_customers():
customer_total = 0
print("£50 an Adult")
askadult = int(input("How many adults? "))
customer_total = askadult * 50
print("total: £", customer_total)
print("£25 a Child")
askchild = int(input("How many children? "))
customer_total = (askchild * 25) + customer_total
print("total: £", customer_total)
return customer_total, askadult, askchild
# number of nights (multiplier)
def num_nights(customer_total):
nights_total = 0
waiting = True
while waiting == True:
try:
asknights = int(input("How many nights are you staying for? "))
nights_total = customer_total * asknights
print("total: £", nights_total)
break
except ValueError:
print("invalid input!")
return nights_total, asknights
# New Booking *********
def new_booking():
askname = str(input("Please enter your name? "))
idnumber = random.randint(100, 999)
customer_total, numAdults, numChild = num_customers()
Num_people = numAdults + numChild
nights_total, asknights = num_nights(customer_total)
askbar = mini_bar()
askbreakfast = breakfast(Num_people, asknights)
askdinner = dinner(Num_people, asknights)
total = askdinner + askbreakfast + askbar + asknights
detailslist = (idnumber, askname, numAdults, numChild, asknights, askbar, askbreakfast, askdinner)
for i in detailslist:
f = open('newbooking.txt', 'w')
f.write(str(detailslist) + '\n')
print(i)
print("your total amount is: £", total)
print("your Name & ID number is: ", askname, idnumber)
# Pre booking ***** is not defind
def pre_booked():
name = input("enter your name or ID number: ")
if name == (askname) or (idnumber):
detailslist = [idnumber, askname, askadult, askchild, asknights, askbar, askbreakfast, askdinner]
for i in detailslist:
print(i)
print("total: £", total)
# main menu, start of program.
def main_menu():
print("##################### WELCOME TO BAY HOTEL ###########################")
print('''Please see what is available at the Hotel,\nAdult Prices per night: £50pp,\nChild price: £25pp,\nMiniBar price: £50 per room,\nBreakfast: £20pp,\nDinner: £25pp''')
while True:
prebook = input("Have you booked? Y/N")
if prebook.upper() == "N":
new_booking()
elif prebook.upper() == "Y":
pre_booked()
main_menu()
- I expect the output of #def new_booking to be Keep data in files continuously but the actual output is keep data in files just one sentence
- I expect the output of #def pre_booked to be Extract data from file but the actual output is "NameError: name 'askname' is not defined"
这里的问题是,您实际上还没有在
pre_booked()
函数中定义askname,因此无法与之进行比较。在new_booking()
的情况下,您要求用户名带有askname = str(input("Please enter your name? "))
,但是在pre_booked()
的情况下,您不需要这样做,因此您不能在没有从某处获得值的情况下在那里使用它看到您将
new_booking()
保存到一个文件中,您可能希望像这样从文件中加载数据:在新的预订功能中,最好将所有相关数据逐行放入,甚至可以使用dicts,以便以后确定哪些值属于同一行,而不是将所有值写入各自的行中。所以你可以这样做:
然后你可以一行一行地读,并可能使用´评估()´ 要从字符串中获取列表或字典,或者至少要知道一行中的值在以后属于同一行
您可能还需要考虑在bookings文件中使用“a”作为append,而不是w作为write,这取决于您希望在文件中包含多个booking值还是仅包含一个值
我想这是因为你在文件中写入数据的方式
使用文件有3个步骤:
1)打开文件
2)读取[从]/写入[到]文件
3)关闭文件
第三步是你没有处理的
您希望在文件中编写一个列表,并且在每次迭代中打开该文件。当你只需要做一次的时候,这不是一个好主意(打开和关闭文件都有它们的开销)
我更改了一些
new_booking
函数,并在此处编写:相关问题 更多 >
编程相关推荐