繁体   English   中英

请问有人可以帮我整理我的代码吗?

[英]Please could someone help me to tidy up my code?

这部分代码也有错误

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

当我询问额外的景点时,即使我输入“是”作为输入,它也不会让我进入下一个问题,我不知道为什么。 这段代码是我的一个编码项目,所以任何对此的帮助将不胜感激,(对不起,如果我做错了什么:我是新来的 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")

对于您的错误,您定义了一个 function,但您不运行它。 像这样修复它:

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

更重要的是,我认为您不了解函数的工作原理。 看看这个链接寻求帮助。 您的 function 会自行返回,这看起来像是不应该发生的事情(感谢 9769953 对此发表评论)。 没有为您的 function 提供 return 声明是完全可以的,如下所示:

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'")

为了整理你的代码,看起来没有太多事情要做:你的大部分代码都是打印语句。 如果您愿意,您可以列出您打印的所有内容,但我不建议这样做。 (以下示例):

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'))

这只是清理打印语句的一种方法,您可以使用.format()变量,如图所示。 同样,我不推荐这个,因为它很乱而且很难编辑。

如果你想清理代码,你应该考虑删除 deftraction def attraction() function,然后将代码粘贴到 if 块中,如下所示:

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'")

你也可以重新格式化你的try-except块,但它只会使缩进更干净,它不会改变代码长度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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