我正在编写一个互联网服务提供商计划。我的代码存在问题,因为它无法正确打印月度帐单。
例如: 如果用户输入包裹“ A”和使用的小时数(例如9个小时) 然后在调用函数printBill时应该打印9.95
我的问题:如何将数据从getPackage()调用到函数printBill()
#Bill for Package A
def getPackageA(hours):
if (hours < 10):
return 9.95 #Cost of Package A
else:
return (hours-10)*2 + 9.95
#Bill for Package B
def getPackageB(hours):
if (hours < 20):
return 13.95 #Cost of Package B
else:
return (hours - 20) + 13.95
#Bill for Package C
def getPackageC():
return 19.95 #Cost of Package C
#Print Bill and savings
def printBill(bill):
if (bill != 0):
print("Your monthly bill is $", format(bill, '.2f'),
sep = '')
getSavings(bill)
print()
print()
else:
print()
#Checks and display savings if applicable
def getSavings(bill):
if(bill > getPackageA(hours)):
print("If you had package A, you'd save $",\
format(bill - getPackageA(hours),'.2f'), sep =
'')
if(bill > getPackageB(hours)):
print("If you had package B, you'd save $",\
format(bill - getPackageB(hours),'.2f'), sep =
'')
if(bill > getPackageC()):
print("If you had package C, you'd save $",\
format(bill - getPackageC(), '.2f'), sep = '')
def main():
bill = 1
#Asks user to enter choice of package and hours used
packageChoice = str(input("Enter package purchased (A,
B, or C): "))
hours = int(input("Enter the number of hours used: "))
if(packageChoice == 'A' or packageChoice == 'a'):
getPackageA(hours)
elif (packageChoice == 'B' or packageChoice == 'b'):
getPackageB(hours)
elif (packageChoice == 'C' or packageChoice == 'c'):
getPackageC()
else:
print("Package choice must be A, B, or C.")
printBill(bill)
main()
答案 0 :(得分:3)
您可以将多个参数传递给一个函数。
def printBill(bill)
成为:
def printBill(bill,hours):
您用以下方式调用它:
printBill(bill,hours)
您还必须以相同的方式将其传递给getSavings。
答案 1 :(得分:1)
您需要将小时数传递给需要使用它的每个函数,而且当您返回某些东西时,它也需要一个返回的地方。因此,您说的是如果小时数小于10,则返回9.95,但是当您说return时,它会将代码发送回被调用的位置,您没有将其分配给变量,因此bill默认为$ 1。这是更新的代码,可以正常工作
#Bill for Package A
def getPackageA(hours):
if hours < 10:
return 9.95 #Cost of Package A
else:
return (hours-10)*2 + 9.95
#Bill for Package B
def getPackageB(hours):
if hours < 20:
return 13.95 #Cost of Package B
else:
return (hours - 20) + 13.95
#Bill for Package C
def getPackageC():
return 19.95 #Cost of Package C
#Print Bill and savings
def printBill(bill, hours):
if (bill != 0):
print("Your monthly bill is $", format(bill, '.2f'), sep='')
getSavings(bill, hours)
print('\n')
#Checks and display savings if applicable
def getSavings(bill, hours):
if bill > getPackageA(hours):
print("If you had package A, you'd save $",\
format(bill - getPackageA(hours),'.2f'), sep='')
if bill > getPackageB(hours):
print("If you had package B, you'd save $",\
format(bill - getPackageB(hours),'.2f'), sep='')
if bill > getPackageC():
print("If you had package C, you'd save $",\
format(bill - getPackageC(), '.2f'), sep='')
def main():
bill = 1
#Asks user to enter choice of package and hours used
packageChoice = str(input("Enter package purchased (A, B, or C): "))
hours = int(input("Enter the number of hours used: "))
if packageChoice in ('a', 'A') :
bill = getPackageA(hours)
elif packageChoice.lower() == 'b':
bill = getPackageB(hours)
elif packageChoice.upper() == 'C':
bill = getPackageC()
else:
print("Package choice must be A, B, or C.")
printBill(bill, hours)
main()
我还编辑了main()函数,向您展示了检查响应的不同方法。您也不需要在IF语句的Python中用括号将内容包装起来。
答案 2 :(得分:0)
要回答您的问题,只需执行以下操作:
bill = 1
bill += getPackage() # call function and add return value to total bill
printBill(bill)