[英]Name error when calling function asking for input from user
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)))
I keep getting an error saying that city is not defined. 我不断收到错误消息,指出未定义城市。 I am new to Python so I am sorry if this is question that is easy to answer.
我是Python的新手,如果这是一个容易回答的问题,对不起。 All the variables already have their set functions.
所有变量已经具有其设置功能。 Here is city's one just in case it helps
以防万一
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")
Also this is an altered code from Code Academy 这也是代码学院的修改代码
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())
Get rid of the parameters; 摆脱参数; you do not need them since you will get it from user input via the
input()
function. 您不需要它们,因为您可以通过
input()
函数从用户输入中获取。
I used the int
function to convert the input of numbers which are strings to integers. 我使用了
int
函数将数字输入转换为字符串。
In your print((trip_cost(city,days,spending_money)))
call, where is city coming from? 在您的
print((trip_cost(city,days,spending_money)))
通话中,城市是哪里来的? It looks like it is outside any of the functions so you need to declare it somewhere as something. 看起来它在任何函数的外部,因此您需要在某处将其声明为某种东西。 And even when you do so, you will get the same error for
days
and spending_money
. 甚至当你这样做,你会得到同样的错误了
days
和spending_money
。 These need to be declared before they are printed. 这些需要在打印之前声明。
Either that or actually pass in values to your trip_cost
call in the print statement :) 该值或实际上将值传递给print语句中的
trip_cost
调用:)
Also, looking at the code more closely, it looks like your trip_cost method does not even need any arguments. 另外,仔细查看代码,您的trip_cost方法看起来甚至不需要任何参数。 you are creating the variables when asking for input so it looks like they are redundant as parameters.
您在请求输入时正在创建变量,因此看起来它们作为参数是多余的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.