为什么我们不能在函数中使用raw_input?

时间:2018-05-23 14:16:01

标签: python

以下代码的错误有问题: - (我想这样做,我们可以输入值,该值可以作为变量)

def hotel_cost(nights):
  return 140 * nights

def plane_ride_cost(city):
  if city == "Charlotte":
    return 183
  elif city == "Tampa":
    return 220
  elif city == "Pittsburgh":
    return 222
  elif city == "Los Angeles":
    return 475

def rental_car_cost(days):
  cost = days * 40
  if days >= 7:
    cost -= 50
  elif days >= 3:
    cost -= 20
  return cost

def trip_cost(city, days, spending_money):
  return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money


city = raw_input("Which city will you stay in ")
days = raw_input("How long is the stay ")
spending_money = raw_input("How much is your spening budget ")


print trip_cost(city, days, spending_money)

显示错误:

Traceback (most recent call last):
  File "python", line 31, in <module>
  File "python", line 23, in trip_cost
  File "python", line 17, in rental_car_cost
TypeError: unsupported operand type(s) for -=: 'str' and 'int'

1 个答案:

答案 0 :(得分:2)

您需要在此使用intfloat

city = raw_input("Which city will you stay in ")
days = int(raw_input("How long is the stay "))
spending_money = float(raw_input("How much is your spening budget "))