简体   繁体   English

调用函数要求用户输入时出现名称错误

[英]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 . 甚至当你这样做,你会得到同样的错误了daysspending_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.

相关问题 function 不要求用户输入 python - function not asking for user input python 在 Python 中的函数中要求用户输入 - Asking for a user's input in a function in Python 在打印功能中要求用户输入 - Python 2.7 - Asking for user input in print function - Python 2.7 Python 3.10.6:调用用户定义的 function 时出现语法错误和名称错误 - Python 3.10.6: Syntax error and Name error when calling a user defined function 调用函数进行打印时,如何从我的void函数中获取用户输入? - when calling a function to print, how do i get it to get user input from my void function? 要求用户从他们的计算机输入文件 - Asking user to input a file from their computer 尝试编写要求用户输入输入文件名的python代码,它可以工作,但是当我输入输入文件时却说未定义“输入文件”? - Trying to write python code asking user for the name of the input file, it works but when I enter the input file it says 'input file' is not defined? 递归调用函数以供用户输入 - Calling a function recursively for user input 询问用户输入,运行指定功能,然后返回询问 - Ask for user input, run specified function, then go back to asking Python解释器完全跳过了要求用户输入的功能? - Python interpreter completely skips function asking for user input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM