def trip_cost(city,days,spending_money):
days = input("Enter amount of days for car hire")
city = input("City name")
days = input("Nights staying")
spending_money = input("Spending money")
return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + spending_money
print((trip_cost(city,days,spending_money)))
我一直收到错误消息,说明城市没有定义。我是Python的新手,所以如果这个问题很容易回答,我很抱歉。所有变量都已设置其功能。这是城市的一个,以防它有帮助
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 input("Please try again")
这也是Code Academy改进的代码
答案 0 :(得分:2)
def trip_cost():
days = int(input("Enter amount of days for car hire"))
city = input("City name")
nights = int(input("Nights staying"))
spending_money = int(input("Spending money"))
return hotel_cost(nights) + plane_ride_cost(city) + rental_car_cost(days) + spending_money
print(trip_cost())
摆脱参数;您不需要它们,因为您可以通过input()
函数从用户输入中获取。
我使用了int
函数将字符串输入的数字转换为整数。
答案 1 :(得分:1)
在print((trip_cost(city,days,spending_money)))
来电中,城市来自哪里?看起来它不在任何函数之外,所以你需要将它声明为某个东西。即使您这样做,days
和spending_money
也会出现相同的错误。这些需要在打印之前声明。
或者实际将值传递给print语句中的trip_cost
调用:)
此外,更仔细地查看代码,看起来你的trip_cost方法甚至不需要任何参数。您在请求输入时创建变量,因此看起来它们是冗余的参数。