【问题标题】:Getting unexpected output in the if and elif statement of userinput在 userinput 的 if 和 elif 语句中得到意外的输出
【发布时间】:2021-09-30 09:49:19
【问题描述】:

我正在尝试使用 python 制作一个 OTP 生成器和验证器。但是我没有得到想要的输出,即使我输入了错误的 OTP,它也会考虑第一个打印语句并打印“欢迎先生”。这不仅发生在这个项目上——每个项目都会发生在我身上。我在这里做错了什么?

这是我的代码:

import math, random
import smtplib

def sendEmail(to, content):
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.login('your mail', 'your password')
    server.sendmail('your mail', to, content)
    server.close()#please allow less secure app to access this

# function to generate OTP
def generateOTP() :
    # Declare a digits variable
    # which stores all digits
    digits = "0123456789"
    OTP = ""

# length of password can be chaged
# by changing value in range
    for i in range(4) :
        OTP += digits[math.floor(random.random() * 10)]
    return OTP

# Driver code
if __name__ == "__main__" :
    #print(generateOTP())
    content =   generateOTP() 
    to = 'your mail'
    sendEmail(to, content)
    print('mail sent !!')

    userinput = input('enter your otp')
    if (userinput!= generateOTP()):
        print('welcome sir')
    else:
        print('otp not recognized please enter valid otp!')

【问题讨论】:

  • 您正在generateOTP 中生成一个随机字符串,您认为您输入的输入是否正确?
  • 您正在检查是否不相等(用户输入!= generateOTP)。但这里的逻辑是什么?您生成一个新的 OTP 并与输入进行比较。我不明白你的代码的目的
  • content不会等于if语句中generateOTP的返回值,会在那里生成一个新的OTP

标签: python one-time-password


【解决方案1】:

你可以修改这段代码:

userinput = input('enter your otp')
if (userinput == content):
    print('welcome sir')
else:
    print('otp not recognized please enter valid otp!')

【讨论】:

    【解决方案2】:

    你在 python 中混淆了!===

    != 表示不等于== 表示等于

    该语句将检查用户输入的值是否不等于generateOTP()返回的值。只要用户输入不匹配,userinput!= generateOTP() 的计算结果为 True 并显示 "welcome sir"

    if (userinput!= generateOTP()):
    

    假设我输入'1234',生成的OTP为'1243'if (userinput!= generateOTP()):为True,因为'1234'不等于'1243'

    你的意思可能是:

    if userinput== generateOTP():
    

    【讨论】:

      【解决方案3】:

      generateOTP() 函数每次调用都会生成一个新的 OTP。 因此,即使用户输入了正确的 OTP,它也不会与原始 OTP 进行验证,而是生成一个新的 OTP 并将用户输入的 OTP 与它进行比较,这永远不会相等。自从

      print('欢迎先生')

      语句在 OTP 不匹配时执行,它总是被触发。

      试试这个

      userinput = input('enter your otp')
      if (userinput== content):
          print('welcome sir')
      else:
          print('otp not recognized please enter valid otp!')
      

      【讨论】:

      • 它有效,但是当我在内容中写任何其他内容时我该怎么办?
      • 内容是正在生成的字符串。您可以将任何其他内容附加到您的字符串示例: content = "您生成的 OTP 是 " + content + " 。请在字段中输入 OTP 以进行验证。"
      【解决方案4】:

      您的第一个 if 条件检查是否为 userinput != generateOTP()!= 比较运算符表示not equal to。所以基本上,您的 if 语句表示如果用户的输入不等于生成的 OTP,则打印“welcome sir”,这与您要实现的逻辑相反。

      以下代码将为您解决问题。您需要做的就是将!= 替换为==

      if (userinput == generateOTP()):
              print('welcome sir')
      else:
          print('otp not recognized please enter valid otp!')
      

      但同样,generateOTP 返回一个随机字符串。所以我不明白这段代码有什么意义。用户不知道要输入什么输入才能正确。

      【讨论】:

      • 这不会解决任何问题,因为 GenerateOTP 每次调用时都会生成一个随机 OTP。所以用户必须猜测一个随机数......
      • 是的@jps 我在发布答案后就想通了
      • 你去userinputcontent核对,@FloLie已经回答了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多