簡體   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