在Codecademy的本课程中添加raw_inputs

时间:2018-07-13 18:24:09

标签: python

这里的初学者刚刚开始在Codecademy上自学python。我对完成的锻炼有疑问

city = raw_input("where are you going? ")
days = raw_input("how many days are you staying? ")
spending_money = raw_input("how much spending money are you bringing? ")

上面有关计划行程的代码对我来说很有意义。在继续下一课之前,我想尝试找点乐子是如何添加raw_inputs,这样我可以像这样

,而不是在最后一行输入print trip_cost中的值
class Student(models.Model):
    name = models.CharField(...)
class Subject(models.Model):
    subject = models.CharField(...)

class Attendance(Student, Subject):
    att = model.IntegerField()

我似乎无法弄清楚如何将其集成到代码中。有什么帮助吗?我知道这是超级基础,但是只是弄湿了我的脚。

1 个答案:

答案 0 :(得分:0)

您几乎正确,只需添加以下内容即可:

def hotel_cost(days):
  return 140 * (days - 1)

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
  else:
    return 0

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 - 1) + plane_ride_cost(city) + spending_money

city = raw_input("where are you going? ")
days = int(raw_input("how many days are you staying? "))
spending_money = int(raw_input("how much spending money are you bringing? "))
print trip_cost(city, days, spending_money)