【问题标题】:Having trouble in python functions assignment在 python 函数分配中遇到问题
【发布时间】:2018-06-06 12:19:10
【问题描述】:

我正在使用 codecademy 的 Python,但在计划假期作业 (7/7) 时遇到了麻烦。

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 and days <7:
    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('city?')
days = raw_input('days?')
spending_money = raw_input('money?')
print trip_cost(city,days,spending_money)

当我运行脚本并在提示符下输入时,它无法显示以下错误:

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

【问题讨论】:

  • 遇到麻烦并不能帮助我们帮助您。你有什么问题?你有错误吗?如果你是他们是什么?
  • 遇到了麻烦——你很迷茫。您的问题到底是什么,输入参数的预期结果是什么,实际输出是什么。 - 另外:如果你使用print 而不使用()(和raw_input()),为什么标记为python-3.x,为什么标记printf - 两个标记都是错误的。
  • 一个潜在的问题是输入是tampa(小写)还是未列出的城市。您将需要默认返回并检查城市/输入的情况

标签: python function return


【解决方案1】:

raw_input 返回一个字符串,你不能用字符串做数学运算。您需要将适当的值转换为数字,例如:

days = int(raw_input('days?'))
spending_money = float(raw_input('money?'))

【讨论】:

    【解决方案2】:

    这里有一些问题,但首先,如果你的任务允许,我会使用一个类而不是多个函数。我会简单地创建 1 个大对象。原因是您有太多参数,因此将参数传递给类然后允许函数操作它们而不是将参数传递给多个函数更有意义。一切都是相似的,因为它都与旅行有关,所以我将它存储在一个类中。我使用的是 Python 3.6,因此这与您使用的 Python 2 版本略有不同,但正如您所见,我仍然必须将用户输入转换为整数值。在您的代码中,您没有将用户输入转换为整数值,而是尝试对数据执行数学运算。这也应该给你一个回溯。您必须在 raw_input 之前实际定义数据类型。输入函数在 Python 2 中不会像在 Python 3.6 中那样工作。但是,如果您曾经用 Java 编写过代码,那本质上就像声明一个变量一样。此外,当在这里获取用户输入时,我会更改城市以使用 .lower() 方法评估值。用大写来信任用户是有风险的。我在这里做了一些其他的改变,希望能有所帮助。

    class BigTrip():
    
       def __init__(self, nights, days, city, spending_money):
    
          self.nights = nights
          self.days = days
          self.city = city
          self.spending_money = spending_money
    
       def hotel_cost(self):
          self.hotel_cost = 140 * self.nights
          return "Your hotel cost will be " + str(self.hotel_cost) + " dollars."
    
       def plane_ride_cost(self):
    
          if city.lower() == 'charlotte':
              self.plane_cost = 183
          elif city.lower() == 'tampa':
              self.plane_cost = 220
          elif city.lower() == 'pittsburgh':
              self.plane_cost = 222
          elif city.lower() == 'los angeles':
              self.plane_cost = 475
          return "Your plane cost will be " + str(self.plane_cost) + " dollars."
    
       def rental_car_cost(self):
    
    
          self.rental_cost = self.days * 40
          if int(self.days) >= 7:
              self.rental_cost -= 50
          elif int(self.days) >= 3 and int(self.days) <7:
              self.rental_cost -= 20
          return "Your rental car cost will be " + str(self.rental_cost) + " dollars."
    
       def trip_cost(self):
          return "Your total trip cost will be: {} dollars".format(self.days + self.hotel_cost + self.plane_cost + self.spending_money)
    
    
    nights = int(input("How many nights will you stay in the city?: "))
    
    city = input('\nSelect a city[Charlotte, Tampa, Pittsburgh or Los Angeles]: ')
    
    days = int(input('How many days will you be staying?: '))
    
    spending_money = int(input('How much spending money do you have?: '))
    
    my_trip = BigTrip(nights, days, city, spending_money)
    print(my_trip.hotel_cost())
    print(my_trip.plane_ride_cost())
    print(my_trip.rental_car_cost())
    print(my_trip.trip_cost())
    

    这是你的输出:

    How many nights will you stay in the city?: 4
    Select a city[Charlotte, Tampa, Pittsburgh or Los Angeles]: charlotte
    How many days will you be staying?: 3
    How much spending money do you have?: 1200
    Your hotel cost will be 560 dollars.
    Your plane cost will be 183 dollars.
    Your rental car cost will be 100 dollars.
    Your total trip cost will be: 1946 dollars
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      • 2014-05-26
      • 1970-01-01
      相关资源
      最近更新 更多