我正在编写一个互联网服务提供商程序。我的代码问题是它没有正确打印每月账单。
例如: 如果用户输入套餐“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()
最佳答案
您可以向函数传递多个参数。
def printBill(bill)
变成:
def printBill(bill,hours):
你可以这样调用它:
printBill(bill,hours)
您还必须以相同的方式将其传递给 getSavings。
关于python - 互联网服务提供商计划,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55033929/