-2

I'm writing code in python 3 and i'm stuck. I need to ask user to give username and password at least 3 times before showing the password . I tried "for" and "try" function but unsuccessful. Please help me to know how to do it.

Thanks in Advance

import os
# Setting dir and Changing it to access files from specific folder

path = 'C:\\Users\\user\\Downloads\\Class Work\\Project Python\\files'

os.chdir(path)

# checking if dir has any files

if len(os.listdir(path)) == 0 :
    with open ('name.txt','w') as Name:
        Name.write(str(input("Name: ")))
    with open('mobile.txt','w') as Mob:
        Mob.write(str(input("Mobile No.: ")))
    with open('email.txt','w') as email:
        email.write(str(input("Email ID: ")))
    with open('password.txt','w') as password:
        password.write(input("password: "))

else:

# Authrntication
    a= str(input("Enter Email ID: "))
    b=input("Enter Password: ")
    with open('email.txt','r') as email:
        x = email.read()
    with open('password.txt','r') as password:
        y= password.read()

    if x==a and y==b:

        # If successful change password
        with open('password.txt','w') as password:
            password.write(input("New Password  "))

      #try 3 times before showing password  

    else:

        # IF not successful Show Password
        # after verification of EMail ID and phone

        c = str(input("Enter Email ID: "))
        d = input("Enter Phone: ")
        with open('email.txt','r') as email:
            s = email.read()
        with open("mobile.txt",'r') as mobile:
            t = mobile.read()

        if c==s and d==t:
            with open('password.txt','r') as password:
                p = password.read()
            print(p)
  • What is wrong with your solution? How is it deficient? Do you suspect any part of it? Can you reduce the example to just the minimum required to illustrate the problem< Please read [mcve]. – wwii Feb 21 '20 at 14:55
  • I'm not been able to write code to ask 3 times before showing him password I added # where I should ask. – Saurabh Mittal Feb 21 '20 at 14:58
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Feb 21 '20 at 14:58
  • None of the methods work as i have to repeat authentication part 2 more times in case email and password don't match – Saurabh Mittal Feb 21 '20 at 16:03

1 Answers1

0

i was using 'for' at the wrong place this is the final version. I used 'for' at the beginning of authentication to create loop. and my problem was solved.

import os
# Setting dir and Changing it to access files from specific folder

path = 'C:\\Users\\Saurabh Mittal\\Downloads\\Class Work\\Project Python\\files'

os.chdir(path)

# checking if dir has any files

if len(os.listdir(path)) == 0 :
    with open ('name.txt','w') as Name:
        Name.write(str(input("Name: ")))
    with open('mobile.txt','w') as Mob:
        Mob.write(str(input("Mobile No.: ")))
    with open('email.txt','w') as email:
        email.write(str(input("Email ID: ")))
    with open('password.txt','w') as password:
        password.write(input("password: "))

else:

# Authentication
    for i in range(3):

        a= str(input("Enter Email ID: "))
        b=input("Enter Password: ")
        with open('email.txt','r') as email:
            x = email.read()
        with open('password.txt','r') as password:
            y= password.read()

        if x==a and y==b:

        # If successful change password
            with open('password.txt','w') as password:
                password.write(input("New Password  "))
                break
        else:
            print("Try again")
    else:

        # IF not successful Show Password
        # after verification of EMail ID and phone

        c = str(input("Enter Email ID: "))
        d = input("Enter Phone: ")
        with open('email.txt','r') as email:
            s = email.read()
        with open("mobile.txt",'r') as mobile:
            t = mobile.read()

        if c==s and d==t:
            with open('password.txt','r') as password:
                p = password.read()
            print(p)