0

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")
TechTorch
  • 33
  • 3

2 Answers2

2

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

CrimsonSage
  • 217
  • 3
  • 12
  • What do you mean by hard coded? – TechTorch Nov 16 '16 at 06:16
  • @TechTorch you are comparing the password to a fixed string "thisisthepassword123". So thus someone can open your script file to see the password if they wanted to and bypass it. Use a MD5, SHA1 or other hashing algorithm to hash the password instead. Check out this library: [link](https://docs.python.org/2/library/hashlib.html) I'll add code samples to illustrate it to my answer. – CrimsonSage Nov 16 '16 at 06:20
  • @TechTorch what version of Python are you using? – CrimsonSage Nov 16 '16 at 06:28
  • @TechTorch using Python 3.5.2 the conditional statement was the only error I got. – CrimsonSage Nov 16 '16 at 06:31
  • @CrimdonSage I am using puthon 3.5.2. – TechTorch Nov 16 '16 at 06:37
  • @TechTorch do you have any code that we need to know about? Also make sure the spaces (indentation of the code is correct). In your code snippet it's correct, just make sure that your script uses the same code – CrimsonSage Nov 16 '16 at 06:40
  • Thank you, the else had been indented so it was past the if. Put it back and it worked with the ==. – TechTorch Nov 16 '16 at 06:49
  • @TechTorch wow that's weird then. For indentation standards recommend using 2 spaces to indicate each block of code. So after the `if` all the lines that need to be executed if the `if` is true should have two spaces, same with the else. But you did it correctly in the example. Are you sure your Python environment is set up correctly and such. I used an online tool to run it and it works 100%. Check out this online tool [Your Script](https://repl.it/EZ1u) – CrimsonSage Nov 16 '16 at 06:50
  • @TechTorch my pleasure man. Glad it works now. Will put solution in my answer. – CrimsonSage Nov 16 '16 at 06:51
0

please add == to compare = is use to assign

Afsal Salim
  • 486
  • 3
  • 14