当我运行此代码时,我得到一个意外的缩进错误,但没有缩进,它说有。我正在使用python 64位和Python 3.我尝试了unindenting所有代码并重新发送它但我得到了同样的错误。我正在尝试创建一个用户可以注册并登录的GUI。
以下是代码:
from tkinter import *
import json
import hashlib
import uuid
from tkinter import messagebox
filename = ("username.json")
filename2 = ("password.json")
def hash_password(password):
# uuid is used to generate a random number
salt = uuid.uuid4().hex
return hashlib.sha256(salt.encode() + password.encode()).hexdigest() + ':' + salt
def check_password(hashed_password, user_password):
password, salt = hashed_password.split(':')
return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()
def sign_up():
adduser = (e3.get())
addpassword = (e4.get())
try:
hashed_password = hash_password(addpassword)
with open (filename, "w") as f_obj:
json.dump(adduser, f_obj)
with open (filename2, "w") as f:
json.dump(hashed_password, f)
#print("Signed up")
messagebox.showinfo("Info", "You have succesfully signed up!")
def log_in():
try:
with open (filename) as f_obj:
data = json.load(f_obj)
except FileNotFoundError :
usernameguess = (e1.get())
passwordguess = (e2.get())
user_attempt = usernameguess
pwd_attempt = passwordguess
with open (filename) as f_obj:
username = json.load(f_obj)
if username == user_attempt:
with open (filename2) as f:
password = json.load(f)
if check_password(password, pwd_attempt):
print("You're into your account")
else:
print("Incorrect password")
else:
print("Incorrect username")
master = Tk()
master.geometry("390x220")
master.iconbitmap('favicon.ico')
master.title("Account Manager : Log In | Sign Up")
Label(master, text="Welcome to the Account Manager \n Sign up or Log in \n Note: Your password is protected by hash encryption").grid(row=0, column=1)
Label(master, text="Log In").grid(row=1, column=1)
Label(master, text="Username").grid(row=2)
Label(master, text="Password").grid(row=3)
Label(master, text="Sign Up").grid(row=5, column=1)
Label(master, text="Username").grid(row=6)
Label(master, text="Password").grid(row=7)
e1 = Entry(master)
e2 = Entry(master)
e3 = Entry(master)
e4 = Entry(master)
e1.grid(row=2, column=1)
e2.grid(row=3, column=1)
e3.grid(row=6, column=1)
e4.grid(row=7, column=1)
Button(master, text='Quit', command=master.quit).grid(row=9, column=0, sticky=W, pady=4)
Button(master, text='Sign Up', command=sign_up).grid(row=8, column=1, sticky=W, pady=4)
Button(master, text='Log In', command=log_in).grid(row=4, column=1, sticky=W, pady=4)
mainloop( )
答案 0 :(得分:3)
错误不一样,但在上一行!
在函数sign_up()
(log_in()
函数之前的函数)中,有try
但不是except:
def sign_up():
adduser = (e3.get())
addpassword = (e4.get())
try:
hashed_password = hash_password(addpassword)
with open (filename, "w") as f_obj:
json.dump(adduser, f_obj)
with open (filename2, "w") as f:
json.dump(hashed_password, f)
#print("Signed up")
messagebox.showinfo("Info", "You have succesfully signed up!")
def log_in():
try:
with open (filename) as f_obj:
...
Python期望缩进except:
语句。因此,错误。
此代码中也存在其他真正的缩进错误,一旦纠正此错误,这些错误就会变得明显。
也许Python应该提供"missing except" error
而不是"indent" error
。