0..0
0..0

Reputation: 59

How can I access a variable before I declare it?

I am a total amateur when it comes to python and I have came across an issue:

if input() == existingUsername:
        print('Sorry, that username is taken, please try again:')

However, I need to access exisingUsername before it is declared, because I've defined it at the bottom of my code. I know you can do this in javascript with the let variable, but how can I do it in python?:

let existingUsername;
//code
existingUsername = 'name'

Upvotes: 1

Views: 985

Answers (1)

emluk
emluk

Reputation: 372

You can't use a variable before declaring it. You could however use an empty string:

existingUsername = ""

# .
# .
# the rest of your program
# .
# .
username = input()
if username == existingUsername:
    print('Sorry, that username is taken, please try again:')
else:
    # assign new username here
    existingUsername = username

Upvotes: 3

Related Questions