为什么不能定义我的first_pay?我的第二天工作得很好



这里的目标是计算销售人员销售两辆车的总薪酬,每辆车都是任何类型的。当他们卖出一辆新车时,他们能赚1500美元。当他们出售二手车时,他们会收取车辆价格5%的佣金。我必须让它提示销售人员输入每辆车的类型和售价。然后,它应该显示销售人员的总销售额和总薪酬。我不明白first_pay可能出了什么问题

firstType = input('What is the first type of vehicle you are selling? ')
firstPrice = int(input('What is the price of the first vehicle? '))
secondType = input('What is second type of vehicle you are selling? ')
secondPrice = int(input('What is the price of the second vehicle? '))
if firstType == 'new':
first_pay = 1500
elif firstType == 'used':
first_pay = firstPrice * .05
if secondType == 'new':
second_pay = 1500
elif secondType == 'used':
second_pay = secondPrice * .05
totalPay = first_pay + second_pay
totalSales = firstPrice + secondPrice
print('Total sales: $', format(totalSales, ',.2f'), sep='')
print('Total pay: $', format(totalPay, ',.2f'), sep='')

程序假定print语句是else-if语句的一部分。这适用于

firstType = input('What is second type of vehicle you are selling? ')
firstPrice = int(input('What is the price of the first vehicle? '))
secondType = input('What is second type of vehicle you are selling? ')
secondPrice = int(input('What is the price of the second vehicle? '))
if firstType == 'new':
first_pay = 1500
elif firstType == 'used':
first_pay = firstPrice * .05
if secondType == 'new':
second_pay = 1500
elif secondType == 'used':
second_pay = secondPrice * .05
totalPay = first_pay + second_pay
totalSales = firstPrice + secondPrice
print('Total sales: $', format(totalSales, ',.2f'), sep='')
print('Total pay: $', format(totalPay, ',.2f'), sep='')

看起来firstType从未定义过,所以if-else块的两个路径都计算为false,导致first_pay未被设置。

最新更新