简体   繁体   English

在函数中执行python变量

[英]executing python variable in function

I'm trying to execute a variable called id_choice after it was called in a function I defined called movie_choice . 我试图在我定义的名为movie_choice的函数中调用变量id_choice后执行该变量。

This is how the function looks like: 该函数如下所示:

def movie_choice():  
    user_input = raw_input("Would you like to serch your movie by IMDB ID or by\ title? ").upper()
if user_input == "ID":
    id_choice = raw_input("Enter imdb ID: ")
    print("Serching . . .")
    return id_choice
elif user_input == "TITLE":
    title = raw_input("Enter movie title: ")
    print("Serching . . .")
    return title
elif user_input == "EXIT":
    sys.exit
else:
    print("Please enter a valid choice. ")
    return movie_choice()

api_key = '1234'
url = "http://www.omdbapi.com/?%s&%s" % (id_choice, api_key)
print (url)

After I defined this function, for example if I'll try to print (id_choice) I will get an error because id_choice is not defined. 定义此函数后,例如,如果我尝试print (id_choice)由于id_choice将会收到错误id_choice How can I solve this problem? 我怎么解决这个问题?

I'm new to coding so I'm sorry if the answer to my question will be obvious to some of you. 我是编码的新手,所以很抱歉,如果我的问题的答案对于某些人来说是显而易见的。 Thanks and appreciate the help! 感谢并感谢您的帮助!

If you want to call 'id_choice' on the outside of your function, 如果您想在函数外部调用“ id_choice”,

You need to define it ... on the OUTSIDE of your function 您需要在函数的外部定义它。

This concept is known as scope . 这个概念称为范围 Read up on it. 继续阅读。 Since the variable, 'id_choice', is defined within the 'movie_choice' function, you can only access it within that function. 由于变量“ id_choice”是在“ movie_choice”函数中定义的,因此只能在该函数中访问它。 In the code below, I've defined it before the 'movie_choice' function. 在下面的代码中,我已经在“ movie_choice”函数之前定义了它。 Your code should work now. 您的代码现在应该可以工作了。

Also make sure you are indenting correctly. 还要确保缩进正确。 Those if statements should be indented to the inside of your function: 这些if语句应缩进函数内部

id_choice = ''

def movie_choice(): 
    title = '' 
    user_input = raw_input("Would you like to serch your movie by IMDB ID or by\ title? ").upper()

    if user_input == "ID":
        id_choice = raw_input("Enter imdb ID: ")
        print("Serching . . .")
        return id_choice
    elif user_input == "TITLE":
        title = raw_input("Enter movie title: ")
        print("Serching . . .")
        return title
    elif user_input == "EXIT":
        sys.exit
    else:
        print("Please enter a valid choice. ")
        return movie_choice()

api_key = '1234'
url = "http://www.omdbapi.com/?%s&%s" % (id_choice, api_key)
print (url)

Additionally, you need to define your other variables OUTSIDE your if statements. 此外,你需要定义你的其他变量OUTSIDE你的if语句。

If there's a possibility that id_choice is never going to be defined, then you cannot possibly try to print a variable that is not defined. 如果有可能永远不会定义id_choice,那么您将无法尝试打印未定义的变量。 But by defining it on the outside of your conditionals (or function), you are ensuring that those variables will always have a default value. 但是,通过在条件(或函数)外部进行定义,可以确保这些变量始终具有默认值。

At the moment, your url has the chance of being incorrect because what if the user does not enter "ID" as the user input? 目前,您的网址有可能不正确,因为如果用户未输入“ ID”作为用户输入怎么办? Think of restructuring your code paths to meet that case. 考虑重组代码路径来满足这种情况。

In conclusion, read up on scope and identation . 最后,请阅读范围标识

kay is right about needing to assign variables in the scope where you are accessing them but that is not the problem here . 凯正确地需要在您访问它们的范围内分配变量,但这并不是问题 If I understand correctly I just think your indentation is off and what you really want this: 如果我理解正确,我只是认为您的缩进不正确,您真正想要的是:

def movie_choice():  
    user_input = raw_input("Would you like to serch your movie by IMDB ID or by\ title? ").upper()
    if user_input == "ID":
        id_choice = raw_input("Enter imdb ID: ")
        print("Serching . . .")
        return id_choice
    elif user_input == "TITLE":
        title = raw_input("Enter movie title: ")
        print("Serching . . .")
        return title
    elif user_input == "EXIT":
        sys.exit
    else:
        print("Please enter a valid choice. ")
        return movie_choice()

movie_selection = movie_choice()
print(movie_selection)

Note: Bear in mind that this method is still not very good because like abarnert said, not all code paths lead to a proper return value ( user_input of EXIT just leads to exiting the program.) 注意:请记住,此方法仍然不是很好,因为就像abarnert所说的那样,并非所有代码路径都会导致正确的返回值(EXIT的user_input只会导致退出程序。)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM