Code Academy (Python) 代码实际上 运行 在 PyCharm 中。有人可以解释吗?
Code Academy (Python) code doesn't actually run in PyCharm. Can someone explain?
这里是"worked"在代码学院教程"TakingAVacation"中的代码:
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 hotel_cost(nights):
return 140*nights
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
print trip_cost("Los Angeles", 5, 600)
但是,当我在 PyCharm 中尝试 运行 时,这不起作用。我使用的是 Python 3,教程使用的是 Python 2,但是 classes 在最后,所以我把它放到了 Python 脚本中:
__author__ = 'Kvothealar'
class TakingAVacation:
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 hotel_cost(nights):
return 140*nights
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
print(trip_cost("Los Angeles", 5, 600))
给我一个错误,"rental_car_cost" 未定义。在 class 中使用这些函数是否有问题?我还被告知我应该使用 "self" 作为每个函数中的第一个参数。如果第一个参数是 "self",我应该如何调用这些函数。我在尝试时也遇到了错误。
是的,成员函数需要self
才能引用同一个class中的另一个成员函数。另一个错误是那些成员函数不是静态的,所以你必须在调用它的成员函数之前创建一个 TakingAVacation
的实例。
class TakingAVacation:
def plane_ride_cost(self, city):
if city == "Charlotte":
return 183
elif city == "Tampa":
return 220
elif city == "Pittsburgh":
return 222
elif city == "Los Angeles":
return 475
def hotel_cost(self, nights):
return 140*nights
def rental_car_cost(self, days):
cost = days*40
if days>=7:
cost -= 50
elif days>=3:
cost -= 20
return cost
def trip_cost(self, city, days, spending_money):
return self.rental_car_cost(days) + self.hotel_cost(days) + self.plane_ride_cost(city) + spending_money
holiday = TakingAVacation()
print(holiday.trip_cost("Los Angeles", 5, 600))
这里是"worked"在代码学院教程"TakingAVacation"中的代码:
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 hotel_cost(nights):
return 140*nights
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
print trip_cost("Los Angeles", 5, 600)
但是,当我在 PyCharm 中尝试 运行 时,这不起作用。我使用的是 Python 3,教程使用的是 Python 2,但是 classes 在最后,所以我把它放到了 Python 脚本中:
__author__ = 'Kvothealar'
class TakingAVacation:
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 hotel_cost(nights):
return 140*nights
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
print(trip_cost("Los Angeles", 5, 600))
给我一个错误,"rental_car_cost" 未定义。在 class 中使用这些函数是否有问题?我还被告知我应该使用 "self" 作为每个函数中的第一个参数。如果第一个参数是 "self",我应该如何调用这些函数。我在尝试时也遇到了错误。
是的,成员函数需要self
才能引用同一个class中的另一个成员函数。另一个错误是那些成员函数不是静态的,所以你必须在调用它的成员函数之前创建一个 TakingAVacation
的实例。
class TakingAVacation:
def plane_ride_cost(self, city):
if city == "Charlotte":
return 183
elif city == "Tampa":
return 220
elif city == "Pittsburgh":
return 222
elif city == "Los Angeles":
return 475
def hotel_cost(self, nights):
return 140*nights
def rental_car_cost(self, days):
cost = days*40
if days>=7:
cost -= 50
elif days>=3:
cost -= 20
return cost
def trip_cost(self, city, days, spending_money):
return self.rental_car_cost(days) + self.hotel_cost(days) + self.plane_ride_cost(city) + spending_money
holiday = TakingAVacation()
print(holiday.trip_cost("Los Angeles", 5, 600))