以下是我遇到问题的代码,我似乎无法打印出需要的内容并收到错误:
'non-empty format string passed to object.__format__'
这是代码,我的函数语句似乎是一个问题,它不会进入最终的 displayAns 打印出最后一个语句,通知 谁是 他们是什么类型的驱动程序
def main():
customerName = input('Name of the customer: ')
customerAge = int(input('Age of the customer: '))
customerViolations = int(input('Customer\'s number of traffice violations: '))
price = calcPrice(customerAge, customerViolations)
code = riskCode(customerViolations)
displayAns(customerName, price, code)
def calcPrice(customerAge, customerViolations):
if customerAge < 25:
if customerViolations >= 4:
calcPrice = 480
if customerViolations == 3:
calcPrice = 450
if customerViolations == 2:
calcPrice = 405
if customerViolations == 1:
calcPrice = 380
if customerViolations == 0:
calcPrice = 325
if customerViolations < 0:
calcPrice = 'Invalid Violations Entry'
elif customerAge >= 25:
if customerViolations >= 4:
calcPrice = 410
if customerViolations == 3:
calcPrice = 390
if customerViolations == 2:
calcPrice = 365
if customerViolations == 1:
calcPrice = 315
if customerViolations == 0:
calcPrice = 275
if customerViolations < 0:
calcPrice = 'Invalid Age or Violations Entry'
def riskCode(customerViolations):
if customerViolations >= 4:
riskCode = 'High'
if customerViolations == 3:
riskCode = 'Moderate'
if customerViolations == 2:
riskCode = 'Moderate'
if customerViolations == 1:
riskCode = 'Low'
if customerViolations == 0:
riskCode = 'No'
if customerViolations < 0:
riskCode = 'Invalid Violations Entry'
def displayAns(customerName, price, code):
print(customerName, ' as a ', code, 'risk driver, your insurance will cost $', format(price, '.2f'), sep = '')
main()
答案 0 :(得分:2)
def calcPrice(customerAge, customerViolations):
if customerAge < 25:
if customerViolations >= 4:
calcPrice = 480
执行function_name = value
时,不会从函数返回值。您需要使用return
语句。
def calcPrice(customerAge, customerViolations):
if customerAge < 25:
if customerViolations >= 4:
return 480
if customerViolations == 3:
return 450
#etc
答案 1 :(得分:0)
def main():
customerName = raw_input('Name of the customer: ')
customerAge = int(input('Age of the customer: '))
customerViolations = int(input('Customer\'s number of traffice violations: '))
price = calcPrice(customerAge, customerViolations)
code = riskCode(customerViolations)
displayAns(customerName, price, code)
def calcPrice(customerAge, customerViolations):
if customerAge < 25:
if customerViolations >= 4:
returncalcPrice = 480
if customerViolations == 3:
calcPrice = 450
if customerViolations == 2:
calcPrice = 405
if customerViolations == 1:
calcPrice = 380
if customerViolations == 0:
calcPrice = 325
if customerViolations < 0:
calcPrice = 'Invalid Violations Entry'
elif customerAge >= 25:
if customerViolations >= 4:
calcPrice = 410
if customerViolations == 3:
calcPrice = 390
if customerViolations == 2:
calcPrice = 365
if customerViolations == 1:
calcPrice = 315
if customerViolations == 0:
calcPrice = 275
if customerViolations < 0:
calcPrice = 'Invalid Age or Violations Entry'
def riskCode(customerViolations):
if customerViolations >= 4:
return 'High'
if customerViolations == 3:
return 'Moderate'
if customerViolations == 2:
return 'Moderate'
if customerViolations == 1:
return 'Low'
if customerViolations == 0:
return 'No'
if customerViolations < 0:
return 'Invalid Violations Entry'
def displayAns(customerName, price, code):
print(customerName, ' as a ', code, 'risk driver, your insurance will cost $',format((price), '.2'), ' ')
main()
下面。我认为固定。