Click here to Skip to main content
15,886,049 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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()



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!
Posted
Comments
Sergey Alexandrovich Kryukov 7-Mar-15 0:43am    
The question is: why? This is not how programming works. Accessing a local from outer scope makes no sense, not needed, useless, impossible, wrong idea. You need to start learning programming in general. Right, basic things: functions, variables, types and instances...

By the way, what do you think your tag "globalization" means? :-)

—SA
Blake Allen 7-Mar-15 0:46am    
I have completed the whole python programming course on codecademy.com. What do you mean this is not how programming works? What should I do instead to be able to set the variable player_username and player_password whenever I want without having to type it out?
Sergey Alexandrovich Kryukov 7-Mar-15 1:02am    
You need to formulate you ultimate goal, not "be able to set the variable..." and the like.

1) you cannot use undefined variables; 2) even if you define one, you cannot use it outside its scope; not only it's impossible, it also makes no sense.

Do you seriously think that you can impress anyone by completion of computer course? Too many people here knows well that it means nothing. Especially after your clear demonstration of having no clue on the very basics. At this moment, your knowledge is not zero, it's much less: false knowledge. The sooner you realize it, the better. After a while, you will smite at yourself. Not to worry, it's not hard to learn with time. Especially if you start trusting logic and your own brain, instead of trusting courses. Please, no offense. Believe me, we only want to help you.

—SA
Blake Allen 7-Mar-15 1:12am    
Okay, thank you for telling me how experienced I am. Now is there any way to set the player's username/password and change it quickly without having to type out the whole code again?
Sergey Alexandrovich Kryukov 7-Mar-15 1:18am    
Oh, just please don't get offended...

Look, this is called authentication. It is done only once. No need not only to type it again, but even to check again. You check up if it passes, and them either exit game or continue, and forget the variable.

More seriously, you hardly really need authentication at all. Why? And how you do it? Compare the name with name, password with password?

—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
 
Share this answer
 
Comments
Blake Allen 8-Mar-15 22:29pm    
Thank you!
Sergey Alexandrovich Kryukov 8-Mar-15 22:48pm    
You are welcome.
—SA
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900