我一直在尝试制作一个程序,该程序允许用户购买票证,然后将这些票证的记录保存在 csv 文件中。
我在写入def proceed(1)
中的 csv 文件时遇到问题,收到错误消息:
“未定义名称'amChildTickets'”
我知道我正在尝试从另一个函数调用变量proceed(1)
,但不确定如何使用已在'mainTicketMenu'中定义的变量。
def proceed1():
proceed = input("Would you like to order more tickets? ")
if proceed in ("yes", "Yes", "y", "Y"):
mainTicketMenu()
elif proceed in ("no", "No", "n", "N"):
userNames = input("What are the full names of the people you are ordering tickets for? ")
localTime = time.asctime(time.localtime(time.time()) )
with open("Purchases.csv", "a") as f:
f.write(f"Names: {userNames}, Child tickets: {amChildTickets}, Adult tickets: {amAdultTickets}, Group Adult Tickets: {amGroupAdultTickets}, Family Tivkets: {anFamilyTickets} Date of purchase: {localTime}, UniqueID: {uniqueID}\n")
f.close()
#mainMenu()
def otherTickets1():
otherTickets = input("Would you like to order other tickets? ")
if otherTickets == "Yes" or otherTickets == "yes" or otherTickets == "y" or otherTickets == "Y":
mainTicketMenu()
elif otherTickets == "No" or otherTickets == "no" or otherTickets == "n" or otherTickets == "N":
print(f"Your total cost is £{totalCost}")
totalCost = float(00.00)
def mainTicketMenu():
print(f"{bnum1} Welcome to Fantasialand Theme Park {bnum1}\n\
{bnum2} 1. Buying Adult Tickets {bnum4} \n\
{bnum2} 2. Buying Group adult tickets (2+) {bnum3} \n\
{bnum2} 3. Buying Child Tickets {bnum4} \n\
{bnum2} 4. Buying Family Tickets (10% off Child & Adult prices){bnum5}")
global totalCost
ticketChoice = input(f"Which tickets would you like to purchase? ")
if ticketChoice == "1" or ticketChoice == "adult":
print(f"The price of an adult ticket is £{adultTicket:0.2f}")
amAdultTickets = int(input("How many tickets would you like to purchase? "))
amAdultTicketsCost = amAdultTickets * adultTicket
print(f"The total cost of your adult tickets will be £{amAdultTicketsCost:0.2f}")
purchaseConf = input("Would you like to purchase these tickets? ")
if purchaseConf == "yes" or purchaseConf == "Yes" or purchaseConf == "y" or purchaseConf == "Y":
totalCost = totalCost + amAdultTicketsCost
print(f"Your total cost so far is £{totalCost:0.2f}")
proceed1()
elif purchaseConf == "no" or purchaseConf == "No" or purchaseConf == "n" or purchaseConf == "N":
otherTickets()
elif ticketChoice == "2" or ticketChoice == "Group adult":
print("With a group adult ticket, if you order 2+ adult tickets, \nthe price of adult tickets are reduced")
print(f"The price of an adult ticket for 2+ adults is £{gadultTicket:0.2f}")
amAdultTickets = int(input("How many adult tickets do you want? "))
if amAdultTickets >= 2:
amAdultTicketsCost = amAdultTickets * gadultTicket
print(f"{amAdultTickets} Adult tickets will cost you £{amAdultTicketsCost:0.2f}")
purchaseConf = input("Would you like to purchase these tickets? ")
if purchaseConf == "yes" or purchaseConf == "Yes" or purchaseConf == "y" or purchaseConf == "Y":
totalCost = totalCost + amAdultTicketsCost
print(f"Your total cost so far is £{totalCost:0.2f}")
proceed1()
if purchaseConf == "no" or purchaseConf == "No" or purchaseConf == "n" or purchaseConf == "N":
otherTickets()
elif amAdultTickets < 2:
print("Sorry the group adult ticket is only for 2 or more adults")
elif ticketChoice == "3" or ticketChoice == "child":
print(f"The price of a child ticket is £{childTicket}")
amChildTickets = int(input("How many child tickets would you like to purchase? "))
amChildTicketsCost = amChildTickets * childTicket
print(f"The total cost of your child tickets will be £{amChildTicketsCost}")
purchaseConf = input("Would you like to order these tickets? ")
if purchaseConf == "yes" or purchaseConf == "Yes" or purchaseConf == "y" or purchaseConf == "Y":
totalCost = totalCost + amChildTicketsCost
print(f"Your total cost so far is £{totalCost:0.2f}")
proceed1()
if purchaseConf == "no" or purchaseConf == "No" or purchaseConf == "n" or purchaseConf == "N":
otherTickets()
elif ticketChoice == "4" or ticketChoice == "Family Ticket":
print(f"The price of a child ticket is £{childTicket}")
print(f"The price of an adult ticket is £{adultTicket}")
print(f"With a family ticket, if you order 2+ adult tickets and 2+ child tickets you\nreceive a 10% discount")
amChildTickets = int(input("How many child tickets would you like to purchase? "))
if amChildTickets >= 2:
amChildTicketsCost = amChildTickets * childTicket
totalCost = totalCost + amChildTicketsCost
amAdultTickets = int(input("How many adult tickets would you like to purchase? "))
if amAdultTickets >= 2:
amAdultTicketsCost = amAdultTickets * adultTicket
totalCost = totalCost + amAdultTicketsCost
discount = totalCost * 10 / 100
totalCost = totalCost - discount
print(f"Your total cost with a 10% discount is £{totalCost:0.2f}")
purchaseConf = input("Would you like to purchase these tickets? ")
if purchaseConf == "yes" or purchaseConf == "Yes" or purchaseConf == "y" or purchaseConf == "Y":
proceed1()
if purchaseConf == "no" or purchaseConf == "No" or purchaseConf == "n" or purchaseConf == "N":
otherTickets1()
elif amAdultTickets < 2:
print("Sorry, the family ticket is only for 2 or more adults")
elif amChildTickets < 2:
print("Sorry, the family ticket is only for 2 or more children")
def mainMenu():
print(f"{bnum1} Welcome to Fantasialand Theme Park {bnum1}\n\
{bnum2} 1. Purchasing Tickets For Fantasialand Theme Park {anum1}\n\
{bnum2} 2. Searching For Past Purchases {anum2}")
choice1 = input("What would you like to do? ")
if choice1 == "1" or choice1 == "Purchasing Tickets" or choice1 == "purchasing tickets":
mainTicketMenu()
答案 0 :(得分:0)
有很多方法可以解决这个问题。
当调用proceed1时,票证金额将作为输入传递,然后更新适当的票证类型。当在函数中定义变量时,称为局部变量。范围之外的其他函数不能使用局部变量。但是可以用作其他功能的输入或全局重新定义。该链接将更好地说明。 https://www.geeksforgeeks.org/global-local-variables-python/
#Global ticket list
ticketList = [0,0,0,0]
#inputs: ticket amounts for each type of ticket
def proceed1(amChildTickets,amAdultTickets,amGroupAdultTickets,anFamilyTickets):
# update each ticket amount when called
ticketList[0] += amChildTickets
ticketList[1] += amAdultTickets
ticketList[2] += amGroupAdultTickets
ticketList[3] += anFamilyTickets
proceed = input("Would you like to order more tickets? ")
if proceed in ("yes", "Yes", "y", "Y"):
mainTicketMenu()
elif proceed in ("no", "No", "n", "N"):
userNames = input("What are the full names of the people you are ordering tickets for? ")
localTime = time.asctime(time.localtime(time.time()) )
with open("Purchases.csv", "a") as f:
f.write(f"Names: {userNames}, Child tickets: {ticketList[0]}, Adult tickets: {ticketList[1]}, Group Adult Tickets: {ticketList[2]}, Family Tivkets: {ticketList[3]} Date of purchase: {localTime}, UniqueID: {uniqueID}\n")
f.close()
mainMenu()
def otherTickets1():
otherTickets = input("Would you like to order other tickets? ")
if otherTickets == "Yes" or otherTickets == "yes" or otherTickets == "y" or otherTickets == "Y":
mainTicketMenu()
elif otherTickets == "No" or otherTickets == "no" or otherTickets == "n" or otherTickets == "N":
print(f"Your total cost is £{totalCost}")
totalCost = float(00.00)
def mainTicketMenu():
print(f"{bnum1} Welcome to Fantasialand Theme Park {bnum1}\n\
{bnum2} 1. Buying Adult Tickets {bnum4} \n\
{bnum2} 2. Buying Group adult tickets (2+) {bnum3} \n\
{bnum2} 3. Buying Child Tickets {bnum4} \n\
{bnum2} 4. Buying Family Tickets (10% off Child & Adult prices){bnum5}")
global totalCost
ticketChoice = input(f"Which tickets would you like to purchase? ")
if ticketChoice == "1" or ticketChoice == "adult":
print(f"The price of an adult ticket is £{adultTicket:0.2f}")
amAdultTickets = int(input("How many tickets would you like to purchase? "))
amAdultTicketsCost = amAdultTickets * adultTicket
print(f"The total cost of your adult tickets will be £{amAdultTicketsCost:0.2f}")
purchaseConf = input("Would you like to purchase these tickets? ")
if purchaseConf == "yes" or purchaseConf == "Yes" or purchaseConf == "y" or purchaseConf == "Y":
totalCost = totalCost + amAdultTicketsCost
print(f"Your total cost so far is £{totalCost:0.2f}")
proceed1(0,amAdultTickets,0,0) # pass the amount of tickets to proceed1 function
elif purchaseConf == "no" or purchaseConf == "No" or purchaseConf == "n" or purchaseConf == "N":
otherTickets()
elif ticketChoice == "2" or ticketChoice == "Group adult":
print("With a group adult ticket, if you order 2+ adult tickets, \nthe price of adult tickets are reduced")
print(f"The price of an adult ticket for 2+ adults is £{gadultTicket:0.2f}")
amAdultTickets = int(input("How many adult tickets do you want? "))
if amAdultTickets >= 2:
amAdultTicketsCost = amAdultTickets * gadultTicket
print(f"{amAdultTickets} Adult tickets will cost you £{amAdultTicketsCost:0.2f}")
purchaseConf = input("Would you like to purchase these tickets? ")
if purchaseConf == "yes" or purchaseConf == "Yes" or purchaseConf == "y" or purchaseConf == "Y":
totalCost = totalCost + amAdultTicketsCost
print(f"Your total cost so far is £{totalCost:0.2f}")
proceed1(0,0,amAdultTickets,0)
if purchaseConf == "no" or purchaseConf == "No" or purchaseConf == "n" or purchaseConf == "N":
otherTickets()
elif amAdultTickets < 2:
print("Sorry the group adult ticket is only for 2 or more adults")
elif ticketChoice == "3" or ticketChoice == "child":
print(f"The price of a child ticket is £{childTicket}")
amChildTickets = int(input("How many child tickets would you like to purchase? "))
amChildTicketsCost = amChildTickets * childTicket
print(f"The total cost of your child tickets will be £{amChildTicketsCost}")
purchaseConf = input("Would you like to order these tickets? ")
if purchaseConf == "yes" or purchaseConf == "Yes" or purchaseConf == "y" or purchaseConf == "Y":
totalCost = totalCost + amChildTicketsCost
print(f"Your total cost so far is £{totalCost:0.2f}")
proceed1(amChildTickets,0,0,0)
if purchaseConf == "no" or purchaseConf == "No" or purchaseConf == "n" or purchaseConf == "N":
otherTickets()
elif ticketChoice == "4" or ticketChoice == "Family Ticket":
print(f"The price of a child ticket is £{childTicket}")
print(f"The price of an adult ticket is £{adultTicket}")
print(f"With a family ticket, if you order 2+ adult tickets and 2+ child tickets you\nreceive a 10% discount")
amChildTickets = int(input("How many child tickets would you like to purchase? "))
if amChildTickets >= 2:
amChildTicketsCost = amChildTickets * childTicket
totalCost = totalCost + amChildTicketsCost
amAdultTickets = int(input("How many adult tickets would you like to purchase? "))
if amAdultTickets >= 2:
amAdultTicketsCost = amAdultTickets * adultTicket
totalCost = totalCost + amAdultTicketsCost
discount = totalCost * 10 / 100
totalCost = totalCost - discount
print(f"Your total cost with a 10% discount is £{totalCost:0.2f}")
purchaseConf = input("Would you like to purchase these tickets? ")
if purchaseConf == "yes" or purchaseConf == "Yes" or purchaseConf == "y" or purchaseConf == "Y":
proceed1(0,0,0,amAdultTickets)
if purchaseConf == "no" or purchaseConf == "No" or purchaseConf == "n" or purchaseConf == "N":
otherTickets1()
elif amAdultTickets < 2:
print("Sorry, the family ticket is only for 2 or more adults")
elif amChildTickets < 2:
print("Sorry, the family ticket is only for 2 or more children")
def mainMenu():
print(f"{bnum1} Welcome to Fantasialand Theme Park {bnum1}\n\
{bnum2} 1. Purchasing Tickets For Fantasialand Theme Park {anum1}\n\
{bnum2} 2. Searching For Past Purchases {anum2}")
choice1 = input("What would you like to do? ")
if choice1 == "1" or choice1 == "Purchasing Tickets" or choice1 == "purchasing tickets":
mainTicketMenu()
mainMenu()