简体   繁体   中英

Please could someone help me to tidy up my code?

There is also an error in this part of the code

extraAttractions = input("do you want to know about extra attractions, yes or no? ")
if extraAttractions == "yes":
  def attraction():

when I ask about extra attractions, even when I type yes as my input, it doesn't put me through to the next question and I'm not sure why. This code is for a coding project of mine, so any help on this would be majorly appreciated, (Sorry if I'm doing something wrong: I'm new here D:)

oneAdult = [20.00, 30.00]
oneChild = [12.00, 18.00]
oneSenior = [16.00, 24.00]
familyTicket = [60.00, 90.00]
sixPeoplePlus = [15.00, 22.50]
lionFeeding = 2.50
penguinFeeding = 2.50
eveningBarbeque = 5.00

while True:
  try:
    oneOrTwo = int(input("are you buying tickets for 1 or 2 days? "))
    if oneOrTwo == 1:
      print("day succesfully selected. the prices are, for one adult $", oneAdult[0], "for one child $", oneChild[0], "for one senior $", oneSenior[0], "for a family ticket $",familyTicket[0], "for a group of six people or more(price per ticket) $",sixPeoplePlus[0])
      break;
    elif oneOrTwo == 2:
      print("the prices are, for one adult $", oneAdult[1], "for one child $", oneChild[1], "for one senior $", oneSenior[1], "for a family ticket $",familyTicket[1], "for a group of six people or more(price per ticket) $",sixPeoplePlus[1])
      break;
    else:
      print("your answer needs to be either '1' or '2'")
  except ValueError:
    print("provide this value in integer form")
  continue

extraAttractions = input("do you want to know about extra attractions, yes or no? ")
if extraAttractions == "yes":
  def attraction():
    attractionDay = int(input("Have you booked a ticket for 1 or 2 days"))
    if attractionDay == 1:
      print("The extra attractions are lion feeding for $",lionFeeding,", penguin feeding for $",penguinFeeding,"and the evening barbeque for $",eveningBarbeque)
    elif attractionDay == 2:
      print("The extra attractions are lion feeding for $",lionFeeding,"and penguin feeding for $",penguinFeeding)
    else:
      print("you must input either '1' or '2'")
    
    return attraction()
if extraAttractions == "no":
  print("okay, that's fine")

For your error, you define a function, but you don't run it. Fix it like so:

extraAttractions = input('... ')
if extraAttractions == "yes":
  def attraction():
    # Code
  attraction()

More importantly though, I don't think you understand how functions work. Take a look at this link for help. Your function returns itself, which looks like something that shouldn't happen (thanks to 9769953 for commenting this). It's perfectly fine not to have a return statement for your function, like so:

def attraction():
    attractionDay = int(input("Have you booked a ticket for 1 or 2 days"))
    if attractionDay == 1:
      print(f"The extra attractions are lion feeding for {lionFeeding}, penguin feeding for {penguinFeeding}, and the evening barbeque for {eveningBarbeque}")
    elif attractionDay == 2:
      print(f"The extra attractions are lion feeding for {lionFeeding}, and penguin feeding for {penguinFeeding}")
    else:
      print("You must input either '1' or '2'")

For tidying up your code, it doesn't look like there's too much to do: most of your code is print statements. If you wanted, you could make a list of everything you print, but I don't recommend doing this. (Example below):

list_statements = [
    'thing to print number 1',
    'thing to print number 2',
    'thing to print number {}'
]
print(list_statements[0])
# code
print(list_statements[1])
# code
print(list_statements[2].format('3'))

This is just a way to clean up your print statements, you can use .format() for the variables as shown. Again, I don't recommend this, since it's messy and hard to edit.

If you want code cleanup, you should consider removing the def attraction() function, and just pasting the code into the if block, like so:

extraAttractions = input("do you want to know about extra attractions, yes or no? ")
if extraAttractions == "yes":
    attractionDay = int(input("Have you booked a ticket for 1 or 2 days"))
    if attractionDay == 1:
      print("The extra attractions are lion feeding for $",lionFeeding,", penguin feeding for $",penguinFeeding,"and the evening barbeque for $",eveningBarbeque)
    elif attractionDay == 2:
      print("The extra attractions are lion feeding for $",lionFeeding,"and penguin feeding for $",penguinFeeding)
    else:
      print("you must input either '1' or '2'")

You could also reformat your try-except block, but it'd just make the indentation cleaner, it wouldn't change the code length much.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM