Search code examples
pythonuser-input

My script will not work, not sure if i am doing python right


For some odd reason, my code won't work in Visual Studio on my laptop. It gives me errors on my script. Am I doing it wrong?

The errors I got were:

  • Can't assign to errorexpression --line 2
  • Unexpected indent --line 2
  • Unexpected token '<dedent>' --line 6
print("welcome user")
varpassword = input("Please enter a password:   ")
if varpassword = "thisisthepassword123":
   print("Welcome")
else:
   print("access denied")

Solution

  • As others have pointed out your conditional statement should use the == operator (to indicate that you are comparing the two values to see if they're equal) instead of = that assigns the value to the variable.

    if varpassword = "thisisthepassword123":
    

    I just want to add that you should avoid using a hard-coded password value especially in python since it's plain text (unless this is just sample code to illustrate)

    Edit:

    Use a hashing algorithm to hash your password instead and then hash the user input and compare that. So you'll put the password through something like SHA1 or so (if you want to use a hard-coded value like "thisisthepassword123" it will have a value of f61c1bbcf1f7d68106a18bd753d4fc3c4925793f. So using a library like hashlib(https://docs.python.org/2/library/hashlib.html) you can do this:

    import hashlib
    hashlib.sha1(userinput).hexdigest()
    

    Also consider using salting, read this: https://crackstation.net/hashing-security.htm

    Edit 2:

    Also make sure that your indentation in your script matches the indentation of your code snippet