Pahelp po dito

Status
Not open for further replies.

Yelan

Eternal Poster
Create a program that handles exception and allows writing to and reading from a text file.

Procedure:
1. Create a folder named LastName_FirstName (ex. Reyes_Mark) in your local drive.
2. Create a new project named TaskPerf6. Set the project location to your own folder.
3. Write a program that will:
a. Ask the user to select between two (2) options:
• Register: User shall input his desired username and password. These shall be saved
in a text file named records.txt. Only alphanumeric characters are allowed.
• Login: User is asked to input a username and password. Validate using the text file
created from the Register option. If both the username and password are correct,
display the message “Successfully logged in”, else, display “Incorrect username or
password”.
 
Here's a sample code in Python that handles exceptions and allows writing to and reading from a text file:

Code:
import re

def register():
    username = input("Enter desired username: ")
    password = input("Enter desired password: ")
    if not re.match("^[a-zA-Z0-9]*$", username):
        print("Error: Only alphanumeric characters are allowed for username")
        return
    if not re.match("^[a-zA-Z0-9]*$", password):
        print("Error: Only alphanumeric characters are allowed for password")
        return
    with open("records.txt", "a") as f:
        f.write(f"{username} {password}\n")
    print("Registration Successful")

def login():
    username = input("Enter username: ")
    password = input("Enter password: ")
    with open("records.txt", "r") as f:
        for line in f.readlines():
            record = line.strip().split()
            if record[0] == username and record[1] == password:
                print("Successfully logged in")
                return
    print("Incorrect username or password")

while True:
    choice = input("Select an option:\n1. Register\n2. Login\n")
    if choice == '1':
        register()
    elif choice == '2':
        login()
    else:
        print("Invalid choice")
        continue

In this code, the functions register() and login() are used for the two options. The register() function asks the user for a username and password, validates if they are alphanumeric, and saves them to the text file "records.txt". The login() function asks the user for a username and password, reads from the text file "records.txt" to check if the combination exists, and displays a message accordingly.

The program also uses regular E×ρréššions to validate if the username and password are alphanumeric only. If the input does not match the E×ρréššion, an error message is displayed.

Finally, the program runs in an infinite loop and asks the user for their choice of action. If the input is not valid, the program continues to ask for input.
 
Status
Not open for further replies.

About this Thread

  • 1
    Replies
  • 606
    Views
  • 1
    Participants
Last reply from:
Unknown user

Online now

Members online
959
Guests online
881
Total visitors
1,840

Forum statistics

Threads
2,276,251
Posts
28,968,671
Members
1,231,183
Latest member
Highness08
Back
Top