如何在函数中设置变量并在Python函数外部访问它们? [英] How do I set variables in functions and access them outside of the function in Python?

查看:145
本文介绍了如何在函数中设置变量并在Python函数外部访问它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

My Code:

import time
import sys
authenticating="authenticating"
check_verification="verifying username and password......"



#sets player username and password
print "Welcome to the game."
def set_player_credentials():
    player_username=raw_input("Set username:")
    player_password=raw_input("Set password:")
    for x in check_verification:
        sys.stdout.write(x)
        sys.stdout.flush()
        time.sleep(0.1)
    print "Login Set."
    time.sleep(0.5)



#login screen
print "Username: %s" % (player_username)
print "Password: %s" % (player_password)
check_credentials=raw_input("Is this correct? Y/N")
if check_credentials=="Y" or check_credentials=="y" or check_credentials=="Yes" or check_credentials=="yes":
    print "End of my program for now"
elif check_credentials=="N" or check_credentials=="n" or check_credentials=="No" or check_credentials=="no":
    set_player_credentials()







当我运行此代码时,我收到错误:

Traceback(最近一次调用最后一次):

文件/home/ubuntu/workspace/Game.py,第23行,< module> ;

打印用户名:%s%(player_username)

NameError:名称'player_username'未定义





我希望能够通过调用set_player_credentials函数随时设置变量。我该怎么做呢?谢谢你的帮助!




When I run this code, I get the error:
Traceback (most recent call last):
File "/home/ubuntu/workspace/Game.py", line 23, in <module>
print "Username: %s" % (player_username)
NameError: name 'player_username' is not defined


I want to be able to set the variable whenever I want by calling the set_player_credentials function. How do I do this? Thanks for the help!

推荐答案

让我总结一下:



你必须1)定义变量, 2)如果定义了变量,则不能在外部范围内使用。不仅不可能,而且毫无意义。这些变量存储在调用堆栈中,并且在调用之间根本不存在,在删除帧堆栈后重新使用它的内存。



您可以声明变量在外部范围,但这种做法是非常值得怀疑的。首先,您应该更好地将相关数据封装在类中,创建类实例并将它们作为需要使用的参考传递。



以及关于教育,培训,知识......每个人都需要阅读本文,首先:

Peter Norvig,十年自学编程

http://norvig.com/21-days.html [ ^ ]。



非常鼓舞人心,不是吗? :-)



哪位好运。



-SA
Let me summarize:

You have to 1) define variables, 2) if the variable is defined, it cannot be used in outer scope. Not only it is impossible, but it makes no sense. Such variables are stored on the call stack and simply don't exist between calls, its memory is re-used after the frame stack is removed.

You can declare variable on outer scope, but this practice is very questionable. First, you should better encapsulate related data in classes, create class instance and pass them be reference where they need to be used.

And, as to the education, training, knowledge… Everyone needs to read this, first of all:
Peter Norvig, Teach Yourself Programming in Ten Years,
http://norvig.com/21-days.html[^].

Very encouraging, isn't it? :-)

Which you the best of luck.

—SA


您可以使用基本文件I / O来存储您的详细信息,请参阅 https://docs.python.org/3.3/library/csv.html [ ^ ]一种可能的方式。请记住,.csv文件的内容不安全,因为它是简单的文本,所以任何人都可以阅读它。为了获得适当的安全性,您应该使用某种形式的数据库,但这是一个更高级的主题。
You could use basic file I/O to store your details, see https://docs.python.org/3.3/library/csv.html[^] for one possible way. Remember though that the content of a .csv file will not be secure, as it is simple text, so anyone can read it. For proper security you should use some form of database, but that is a more advanced topic.


这篇关于如何在函数中设置变量并在Python函数外部访问它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆