名称错误 - 名称未定义....但它是

时间:2018-03-25 21:24:56

标签: python nameerror

所以我正在为一堂课写这个小程序,我基本上到了最后,我一直收到错误:

averageWeekMiles = int(totalMiles)/ int(weeksRented)
NameError: name 'weeksRented' is not defined

我已经在我定义daysrented的同一区域定义了它,我无法弄清楚为什么它一直告诉我名字没有定义......

非常感谢任何指导!

rentalCode = input('')
print("(B)udget, (D)aily, or (W)eekly rental?")


if rentalCode == 'D':
  daysRented = input('')
elif rentalCode == 'W':
  weeksRented = input('')



if rentalCode == "D":
  print('Number of Days Rented:')
  print(rentalCode)
elif rentalCode =='B':
  print('Number of Days Rented:')
  print(rentalCode)
elif rentalCode == 'W':
  print('Number of Weeks Rented:')
  print(rentalCode)
if rentalCode == 'D':
  print(daysRented)
elif rentalCode == 'B':
  print(daysRented)
elif rentalCode == 'W':
  print(weeksRented)


budget_charge = 40.00
daily_charge = 60.00
weekly_charge = 190.00  


if rentalCode == 'B':
  baseCharge = daysRented * budget_charge
elif rentalCode == 'D':
  baseCharge = int(daysRented) * int(daily_charge)
elif rentalCode == 'W':
  baseCharge = weeksRented * weekly+charge



print("Starting Odometer Reading:")


odoEnd = input('')
print("Ending Odometer Reading:")

print(odoStart)
print(odoEnd)

totalMiles = int(odoEnd) - int(odoStart)
print(totalMiles)

if rentalCode == 'B':
  mileageCharge = totalMiles * .25


averageDayMiles = int(totalMiles)/ int(daysRented)


if averageDayMiles <= 100:
  extraMiles = 0
elif averageDayMiles > 100:
  extraMiles = averageDayMiles - 100
  mileCharge = .25 * float(extraMiles) * float(daysRented)




averageWeekMiles = int(totalMiles)/ int(weeksRented)

if averageWeekMiles > 900:
  mileCharge = 100 * int(weeksRented)
elif averageWeekMiles <= 900:
  mileCharge = 0


amtDue = baseCharge + mileCharge


print(mileCharge)

1 个答案:

答案 0 :(得分:0)

问题是如果“rentalCode”为“W”,则只定义“weeksRented”。如果rentalCode为“D”,则永远不会定义“weeksRented”,但会使用它,返回错误。