TechTorch TechTorch - 18 days ago 5
Python Question

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

So for some odd reason my code wont work in visual studios on my laptop. it gives me errors on my script. Am i doing it wrong. The errors i got were:


  • cant assign to errorexpression --Line 2

  • unexpedcted indent --Line 2

  • unexpected token '' --line 6

    print("welcome user")
    varpassword = input("Please enter a password: ")
    if varpassword = "thisisthepassword123":
    print("Welcome")
    else:
    print("access denied")


Answer

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

Comments