在python中购买主题公园门票计划

2024-10-02 10:19:05 发布

您现在位置:Python中文网/ 问答频道 /正文

问题

我一直在尝试制作一个程序,允许用户购买门票,然后保存门票的记录,这些门票是在csv文件中购买/记录的。在

我在写入def proceed(1)中的csv文件时遇到问题,收到错误:

"name 'amChildTickets' is not defined"

我知道我正试图从另一个函数调用一个变量到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()

Tags: oroftoyouinputifislike
1条回答
网友
1楼 · 发布于 2024-10-02 10:19:05

有很多方法可以解决这个问题。在

调用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()

相关问题 更多 >

    热门问题