简体   繁体   English

PYTHON:如何创建一个函数来接收变量输入,然后将其发送到另一个函数

[英]PYTHON: how to create a function to receive a variable input and then send it to another function

I'm using Learn Python the Hard Way and exercise 35's extra credit says to simplify. 我正在使用Learn Python the Hard Way并且练习35的额外功劳说简化。 I would like to create a function that will ask the user for variable next and then return it to the other functions. 我想创建一个函数,它将向用户询问下一个变量,然后将其返回到其他函数。

in case I am making no sense... 如果我没有意义......

    def action():
        next = raw_input (">> ")
        return next

    def start():
    print"""
You are in a dark room.
There is a door to your right and left.
Which one do you take?"""

    action()

    if next == "left":
        bear_room()
    elif next == "right":
        cthulu_room()
    else:
        dead("You stumble around the room until you starve.")

when I run it like this it always gives next as else. 当我像这样运行它总是给下一个。

You need to store the return value of the function somewhere; 你需要在某处存储函数的返回值; once it exits, the whole little indented namespace beneath the function goes away, along with the next variable. 一旦它退出,函数下面的整个小缩进命名空间就会随着next变量而消失。 I think you really want: 我想你真的想要:

next = action()

That way, with the function's little namespace destroyed, you will still have a copy of next out at the top level of your program. 这样,当函数的小命名空间被破坏时,您仍然会在程序的顶层有一个next一个副本的副本。

If this feature of Python sounds needlessly destructive, trust me: it is far easier to manage complex programs if you can count on each function being its own little world, that does not make global changes to the variables you have defined! 如果Python的这个特性听起来不必要具有破坏性,请相信我:如果你可以指望每个函数都是它自己的小世界,那么管理复杂程序会容易得多,而不会对你定义的变量进行全局更改!

You need to assign the result of your call to action() in start() to something. 你需要将你的调用结果分配给start() action() Eg next = action() . 例如next = action() When action() is done executing, Python doesn't need the next variable that you created in it anymore, so it discards it. action()执行完毕后,Python不再需要你在其中创建的next变量,因此它会丢弃它。 You can keep that result in another function by assigning the result of the function to a variable (in this case next in the function start() ). 您可以通过将函数的结果赋值给变量(在本例中为函数start()next start()将结果保存在另一个函数中。

Happy Hacking! 快乐黑客!

I've edit your syntax. 我编辑你的语法。 May be it is helping you 可能它在帮助你

def action():
    nextt = raw_input (">> ")
    return nextt

def start():
    print"""
    You are in a dark room.
    There is a door to your right and left.
    Which one do you take?"""

def bear_room():
    print "You meet the bear..."

def cthulu_room():
    print "you meet the princess"

def dead(message):
    print message

def answ(nextt):
    if nextt == "left":
       bear_room()
    elif nextt == "right":
       cthulu_room()
    else:
       dead("You stumble around the room until you starve.")

start()
ok = action()
answ(ok)

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

相关问题 如何在Python中接收字典功能的用户输入? - How to receive user input for Dictionary function in Python? 如何使用python多处理将输入变量传递给另一个函数? - How to pass an input variable into another function with python multiprocessing? 如何将另一个变量输入到 scipy 优化 function - How to input another variable into scipy optimize function 将输入从 function 发送到变量? - send input from function to variable? 如何将变量从一个函数发送到另一个函数? - How do I send a variable from one function to another function? Python-如何判断输入变量是否在函数中重复? - Python - How to judge if input variable is duplicated in a function or not? 如何创建一个 function 应用 function 到另一个 function 在 ZA72117F35426B5937 中的输入 - How to create a function that applies a function to the inputs of another function in Python? 如何定义 function 以基于另一个变量创建具有特定名称的变量? (Python 3) - How can I define a function to create a variable with an especific name based on another variable? (Python 3) 如何通过python函数从另一个python函数获取更新的变量? - How to get updated variable by a python function from another python function? 如何在Python的另一个函数中获取变量? - How to get my variable in another function in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM