Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
683 views
in Technique[技术] by (71.8m points)

random - How to apply OTP in python

I am fairly new to programming and trying to create a program where a user get OTP by using random library and applies to log in to gain access of a file only if it match otherwise permission denied. Not sure what am I doing wrong here as it goes in infinite loop when I run. Below is my code:

def rand_pass(size):
  

generate_pass=''.join([random.choice(string.ascii_uppercase+string.ascii_lowercase+string.digits)for n in range(size)])    
  return generate_pass 

OTP = rand_pass(10)
print('password:'+OTP)

Password = input('enter your OTP here:')

OTP = input('enter your OTP here:')
new_otp = "OTP"


OTP = new_otp
while OTP != "new_otp":
  print('Welcome')

  if OTP == "new_otp":
   print('welcome')
   break
  else:
    print('ACCESS DENIED.')
    continue

Thanks in advance.

question from:https://stackoverflow.com/questions/66057850/how-to-apply-otp-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So as per your requirement you can do as per below code. you just need to keep checking until user enters correct otp. if user enter wrong otp show ACCESS DENIED and ask again to enter otp

import random
import string

def rand_pass(size):
    generate_pass=''.join([random.choice(string.ascii_uppercase+string.ascii_lowercase+string.digits)for n in range(size)])    
    return generate_pass 

generated_otp = rand_pass(10)
print('new OTP :'+generated_otp)

while True:
    OTP = input('enter your OTP here:')
    if OTP == generated_otp:
        print('Correct OTP')
        break
    else:
        print('ACCESS DENIED.')
        continue

@GhostwhiteActualCompilers Please let me know if this is useful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...