为什么不能定义我的first_pay?我的second_pay工作正常

时间:2019-10-28 16:42:32

标签: python python-3.x

此处的目标是计算销售人员出售两辆汽车(每种类型的任何一辆汽车)的总薪资。当他们出售新车时,他们赚了1,500美元。当他们出售二手车时,他们收取车辆价格的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='')

2 个答案:

答案 0 :(得分:0)

程序假定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='')

答案 1 :(得分:-1)

看起来firstType从未定义,因此if else块的两个路径都评估为false,导致未设置first_pay