我正在写一个互联网服务提供商程序。我对代码的问题是它不能正确打印每月账单。你知道吗
例如: 如果用户输入包“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()
你需要把时间花在每一个需要使用它的函数上,而且当你返回一些东西时,它需要一个可以返回的地方。所以你是说如果小时数是<;10,返回9.95,但是当你说return时,它会把代码发送回它被调用的地方,你没有把它赋给一个变量,所以bill默认为1美元。下面是更新后的代码
我还编辑了main()函数以显示检查响应的不同方法。您也不需要在IF语句中用Python的方括号将内容包装起来。你知道吗
可以向函数传递多个参数。你知道吗
变成:
你称之为:
您还必须以同样的方式将其传递给getSavings。你知道吗
要回答您的问题,您只需执行以下操作:
相关问题 更多 >
编程相关推荐