簡體   English   中英

如何讓這些功能打印到屏幕上?

[英]How do I get these functions to print to the screen?

我無法讓我的任何功能返回並將任何內容打印到屏幕上。 我是編程新手,我整天都在嘗試這樣做。 有誰知道我做錯了什么? 我不斷收到一個錯誤,說局部變量'sLastName'已分配給但從未使用過,並且我在 fCost 變量和 fLabor 變量上不斷收到相同的錯誤,但其他變量沒有該錯誤。 所以我想這與為什么它不起作用有關?

# Paint Job Estimator

import math

def main():
    # Get user input
  fWall_Sq_Feet = getFloatInput("Enter wall space in square feet: ")
  fPrice_PerGallon = getFloatInput("Enter paint price per gallon: ")
  fFeet_PerGallon = getFloatInput("Enter feet per gallon: ")
  fLabor_Hours_PerGallon = getFloatInput("How many labor hours per gallon: ")
  fLaborCharge = getFloatInput("Labor charge per hour: ")
  sState = str(input("State job is in: "))
  sLastName = str(input("Customer Last Name: "))

# Call Functions

# Get paint int
  iGetPaint = getGallonsOfpaint(fWall_Sq_Feet, fFeet_PerGallon)

# Labor hours
  fLabor = getLaborHours(fLabor_Hours_PerGallon, iGetPaint)

# Labor cost
  fLaborCost = getLaborCost(fLabor_Hours_PerGallon, fLaborCharge)

# Paint cost
  fPaintCost = getPaintCost(iGetPaint, fPrice_PerGallon)

# State Tax
  fState = getSalesTax(sState)

# Cost estimate
  fCost = showCostEstimate(fLaborCost, fPaintCost, fState)

# Need to get number of gallons of paint
# Return a int and receives 2 floats
def getGallonsOfpaint(fTotalSquareFeet, fFeetPerGallon):
    iGallonsNeeded = math.ceil(fTotalSquareFeet/fFeetPerGallon)
    return iGallonsNeeded

# Returns the labor hours to paint the wall as a float
def getLaborHours(fLaborHoursPerGallon, fTotalGallons):
    return fLaborHoursPerGallon * fTotalGallons

# Returns the labor cost to paint the wall as a float
def getLaborCost(LaborHoursPerGallon, fLaborCharge):
    return LaborHoursPerGallon * fLaborCharge

# Returns the paint cost to paint the wall as a float
def getPaintCost(iGallonsNeeded, fPaintCost):
    return iGallonsNeeded * fPaintCost

# Show cost estimate function
def showCostEstimate(fLaborCost, fPaintCost, fState):
    print("Gallons of paint: ", iGetPaint)
    print("Hours of labor: ", format(fLabor, ".1f"))
    print("Paint charges: ", format(fPaintCost, ".2f"))
    print("Labor charges: ", format(fLaborCost, ".2f"))
    print("Tax: ", format(fState, ".2f"))
    print("Total cost: ", format(fCost, ".2f"))
    return fLaborCost + fPaintCost * fState

# Function for float input
def getFloatInput(sPromptMessage):
    fInput = 0
    while fInput <= 0:
        try:
            fInput = float(input(sPromptMessage))
        except ValueError:
            print("Input must be a number.")
    return fInput

# State function

def getSalesTax(sState):
    if sState == "CT":
         fTaxRate = .06
    elif sState == "MA":
         fTaxRate = .0625
    elif sState == "ME":
         fTaxRate = .085
    elif sState == "NH":
         fTaxRate = .0
    elif sState == "RI":
         fTaxRate = .07
    elif sState == "VT":
         fTaxRate = .06
    else:
        print("Not a valid input")

    return fTaxRate


main()   

因為你沒有使用 print 在主 function 中顯示 output 僅在主 function 的最后使用此代碼:

打印(iGetPaint)
打印(f勞動力)
打印(fLaborCost)
打印(fPaintCost)
打印(fState)
打印(f成本)

這是編輯后的代碼,我將把我發現的錯誤放在下面:

# Paint Job Estimator

import math

def main():
    # Get user input
  fWall_Sq_Feet = getFloatInput("Enter wall space in square feet: ")
  fPrice_PerGallon = getFloatInput("Enter paint price per gallon: ")
  fFeet_PerGallon = getFloatInput("Enter feet per gallon: ")
  fLabor_Hours_PerGallon = getFloatInput("How many labor hours per gallon: ")
  fLaborCharge = getFloatInput("Labor charge per hour: ")
  sState = str(input("State job is in: "))
  sLastName = str(input("Customer Last Name: "))

# Call Functions

# Get paint int
  iGetPaint = getGallonsOfpaint(fWall_Sq_Feet, fFeet_PerGallon)

# Labor hours
  fLabor = getLaborHours(fLabor_Hours_PerGallon, iGetPaint)

# Labor cost
  fLaborCost = getLaborCost(fLabor_Hours_PerGallon, fLaborCharge)

# Paint cost
  fPaintCost = getPaintCost(iGetPaint, fPrice_PerGallon)

# State Tax
  fState = getSalesTax(sState)

# Cost estimate
  fCost = showCostEstimate(fLaborCost, fPaintCost, fState, iGetPaint, fLabor)
  print("Total cost: ", format(fCost, ".2f"))
# Need to get number of gallons of paint
# Return a int and receives 2 floats
def getGallonsOfpaint(fTotalSquareFeet, fFeetPerGallon):
    iGallonsNeeded = math.ceil(fTotalSquareFeet/fFeetPerGallon)
    return iGallonsNeeded

# Returns the labor hours to paint the wall as a float
def getLaborHours(fLaborHoursPerGallon, fTotalGallons):
    return fLaborHoursPerGallon * fTotalGallons

# Returns the labor cost to paint the wall as a float
def getLaborCost(LaborHoursPerGallon, fLaborCharge):
    return LaborHoursPerGallon * fLaborCharge

# Returns the paint cost to paint the wall as a float
def getPaintCost(iGallonsNeeded, fPaintCost):
    return iGallonsNeeded * fPaintCost

# Show cost estimate function
def showCostEstimate(fLaborCost, fPaintCost, fState, iGetPaint, fLabor):
    print("Gallons of paint: ", iGetPaint)
    print("Hours of labor: ", format(fLabor, ".1f"))
    print("Paint charges: ", format(fPaintCost, ".2f"))
    print("Labor charges: ", format(fLaborCost, ".2f"))
    print("Tax: ", format(fState, ".2f"))
    return fLaborCost + fPaintCost * fState

# Function for float input
def getFloatInput(sPromptMessage):
    fInput = 0
    while fInput <= 0:
        try:
            fInput = float(input(sPromptMessage))
        except ValueError:
            print("Input must be a number.")
    return fInput

# State function

def getSalesTax(sState):
    valid = False
    while(not(valid)):
      if sState == "CT":
          fTaxRate = .06
          valid = True
      elif sState == "MA":
          fTaxRate = .0625
          valid = True
      elif sState == "ME":
          fTaxRate = .085
          valid = True
      elif sState == "NH":
          fTaxRate = .0
          valid = True
      elif sState == "RI":
          fTaxRate = .07
          valid = True
      elif sState == "VT":
          fTaxRate = .06
          valid = True
      else:
          print("Not a valid input")
          sState = str(input("State job is in: "))
      return fTaxRate

main()   

好的,這可能不是一個“錯誤”,而只是我修復的一般問題。 對於 getSalesTax function,我添加了一個 while 循環,該循環將繼續接受用戶輸入,直到用戶輸入正確的 state 格式。

對於showCostEstimate(),fCost還沒有定義,所以我們定義好后需要打印出來並調用它。 此外,我們需要在 function 中添加更多參數,因為我們在 function 中調用這些參數。

除此之外,您的代碼看起來非常好。 如果您有任何其他問題/說明,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM