如何解决:(类型错误:“str”对象不可调用)

我正在为一个项目开发一个系统,该系统询问用户是否要创建帐户或登录。第 5 行的“if”条件有问题。整个错误消息是:


Traceback (most recent call last):

  File "main.py", line 5, in <module>

    if login_or_signup("signup"):

TypeError: 'str' object is not callable

编码:


import uuid

import hashlib

login_or_signup = input("would you like to log in? Or signup?: ")


if login_or_signup("signup"):

  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()


  new_pass = input('Please enter a password: ')

  hashed_password = hash_password(new_pass)

  old_pass = input('Now please enter the password again to check: ')

  if check_password(hashed_password, old_pass):

      print('The passwords match!')

  else:

      print('I am sorry but the password does not match')


else:

  print("(NOT CODED YET)")


小唯快跑啊
浏览 204回答 1
1回答

繁花不似锦

正如其他人所提到的,这是一个小错误。您需要在if语句中按以下方式比较字符串:if&nbsp;login_or_signup&nbsp;==&nbsp;"signup":
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python