Làm thế nào để bạn tạo một tên người dùng trong python?

Trong hướng dẫn này, chúng tôi tìm hiểu cách đề xuất tên người dùng bằng cách thêm các ràng buộc trong Python. Chúng tôi đã đặt các ràng buộc sau trước khi tạo tên người dùng

  1. Ít nhất hai chữ in hoa
  2. Chỉ cho phép sử dụng các ký tự đặc biệt . , -_
  3. Có ít nhất 3 chữ số

Tự động đề xuất tên người dùng trong Python

Để làm cho nó có ý nghĩa hơn với người dùng, trước tiên chúng tôi sẽ lấy dữ liệu đầu vào từ người dùng và trên cơ sở dữ liệu đầu vào, chúng tôi sẽ đề xuất tên người dùng cho họ. Hãy xem mã hoàn chỉnh từng bước

Bước 1. Bắt đầu tên người dùng

Giờ đây, tên người dùng chủ yếu bắt đầu từ '#' hoặc '@'. Chúng tôi sẽ giữ phần đầu tên người dùng của mình bằng thẻ bắt đầu bằng # [ # ]. Bạn có thể giữ bất cứ biểu tượng nào bạn muốn

Bước 2. Lấy thông tin người dùng

Rõ ràng, chúng tôi muốn tên người dùng có ý nghĩa nào đó đối với người dùng và người dùng phải liên quan đến nó bằng cách nào đó để giúp họ dễ nhớ hơn

Thông tin đơn giản nhất mà người ta có thể nhận được về một người là tên của họ và ở đây chúng tôi sẽ xem xét tên đầy đủ của người dùng

Bước 3. Thêm các ràng buộc

Bước tiếp theo, chúng tôi sẽ thêm các ràng buộc sau vào mã tạo tên người dùng của chúng tôi

1. Ít nhất hai chữ hoa/chữ hoa

Chúng tôi sẽ sử dụng các chữ cái đầu của tên và họ để đáp ứng ràng buộc này

2. Bổ sung ký tự đặc biệt

Chỉ cho phép 3 ký tự đặc biệt là ‘. ', '-' và '_'

Vì vậy, sau các chữ hoa, chúng ta sẽ chèn một ký tự đặc biệt. Bạn có thể đặt các ký tự ở bất kỳ vị trí nào bạn muốn chỉ cần thay đổi thứ tự của các câu lệnh

3. Ít nhất ba chữ số và một số chữ cái viết thường ngẫu nhiên

Hạn chế cuối cùng là phải có sự kết hợp của các chữ cái viết thường và ít nhất ba chữ số

Số lượng chữ thường phụ thuộc vào độ dài của tên người dùng và trong trường hợp của chúng tôi, chúng tôi sẽ giữ độ dài của tên người dùng là 10

Trong 10 ký tự này, bốn ký tự đã được điền bằng '#', hai ký tự viết hoa và một ký tự đặc biệt

Để các chữ thường đơn giản hơn, chúng tôi sẽ chọn các ký tự ngẫu nhiên từ các chữ cái còn sót lại từ tên của người dùng. Và chúng tôi sẽ chọn ba chữ số ngẫu nhiên từ 0 đến 9

Chúng tôi sẽ giữ thứ tự cuối cùng của tên người dùng như hình dưới đây

# + 2 Uppercase characters + . or - or _ + 3 Lowercase characters + 3 Digits

Tự động đề xuất tên người dùng bằng Python [Triển khai]

Việc triển khai mã hoàn chỉnh được hiển thị bên dưới và các nhận xét được thêm vào để bạn hiểu

# Taking input of name of the user
name  = input["Enter your full name: "]

# Initializing the username
username = "#"

# 1. First two uppercase letter
l = name.split[]
# Name must have both first and last name
while[len[l]!=2]:
    name = input["Enter full name please: "]
    l = name.split[]
username += l[0][0].upper[]
username+=l[1][0].upper[]

# 2. Adding special character [ . , _ or -]
import random
choice = random.choices[".-_", k=1]
username += choice[0]

# 3. Atleast three digits : The 3 digits chosen [ will be added after lowecase letters]
digits_chosen = random.choices["0123456789",k=3] 

# 4. Lowercase letters [ 3 ]
remaining = l[0][1:] + l[1][1:]
letters_chosen = random.choices[remaining,k=3]

# 5. Include the three lower and then three digits
username = username+  letters_chosen[0] + letters_chosen[1] + letters_chosen[2]
username = username + digits_chosen[0] + digits_chosen[1] + digits_chosen[2]

print["The Final Username Generated is: ", username]

đầu ra

Mã đã được kiểm tra cho một số đầu vào ngẫu nhiên. Bạn có thể tự xem chúng

Enter your full name: Isha Bansal
The Final Username Generated is:  #IB-sha914

Và trường hợp người dùng không nhập họ và tên thì chương trình sẽ yêu cầu nhập lại

Enter your full name: Kartik
Enter full name please: Kartik Gupta
The Final Username Generated is:  #KG_iat397

Tôi hy vọng bạn hiểu logic và việc thực hiện vấn đề. Bạn có thể đặt và thay đổi các ràng buộc theo sở thích của riêng mình

Trên mỗi lần lặp lại, chúng tôi nhắc người dùng nhập tên người dùng và mật khẩu và kiểm tra xem các giá trị có đúng không

Nếu bạn muốn ẩn văn bản mật khẩu khi người dùng đang nhập, hãy sử dụng phương thức

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
0

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue

Chúng tôi đã sử dụng phương pháp từ mô-đun

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 để nhắc người dùng nhập mật khẩu mà không lặp lại

Mô-đun

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 có sẵn trong thư viện tiêu chuẩn, vì vậy bạn không phải cài đặt bất cứ thứ gì

Phương pháp

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 thường được sử dụng để nhắc người dùng nhập mật khẩu hoặc thông tin nhạy cảm khác

Nếu cả hai điều kiện trong câu lệnh

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
4 ước tính thành
import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
5, thì khối
import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
4 sẽ chạy khi chúng ta thoát ra khỏi vòng lặp while

Tuyên bố thoát ra khỏi vòng lặp

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
8 hoặc while trong cùng

Nếu một hoặc cả hai điều kiện đánh giá là

import pwinput

password = pwinput.pwinput[prompt='Enter your password: ', mask='*']
print[password]
0, thì khối
import pwinput

password = pwinput.pwinput[prompt='Enter your password: ', mask='*']
print[password]
1 sẽ chạy

Trong khối

import pwinput

password = pwinput.pwinput[prompt='Enter your password: ', mask='*']
print[password]
1, chúng tôi tăng biến
import pwinput

password = pwinput.pwinput[prompt='Enter your password: ', mask='*']
print[password]
3 lên ____13_______4 và tiếp tục đến lần lặp tiếp theo của vòng lặp while

Câu lệnh

import pwinput

password = pwinput.pwinput[prompt='Enter your password: ', mask='*']
print[password]
6 tiếp tục với lần lặp tiếp theo của vòng lặp

Nếu người dùng nhập sai thông tin xác thực 3 lần, biến

import pwinput

password = pwinput.pwinput[prompt='Enter your password: ', mask='*']
print[password]
3 được đặt thành
import pwinput

password = pwinput.pwinput[prompt='Enter your password: ', mask='*']
print[password]
8 và điều kiện trong vòng lặp while không còn được đáp ứng

Hiển thị dấu hoa thị khi người dùng nhập mật khẩu của họ bằng Python

Để hiển thị dấu hoa thị khi người dùng nhập mật khẩu của họ

  1. Cài đặt mô-đun
    import getpass
    
    password = getpass.getpass['Enter your password: ']
    print[password]
    
    0
  2. Sử dụng phương pháp
    import getpass
    
    password = getpass.getpass['Enter your password: ']
    print[password]
    
    0 để lấy đầu vào từ người dùng và hiển thị dấu hoa thị
  3. Phương thức lấy một đối số
    import getpass
    
    password = getpass.getpass['Enter your password: ']
    print[password]
    
    2 có thể được đặt thành dấu hoa thị

Đảm bảo cài đặt mô-đun

import getpass

password = getpass.getpass['Enter your password: ']
print[password]
3 bằng cách mở trình bao của bạn trong thư mục gốc của dự án và chạy lệnh
import getpass

password = getpass.getpass['Enter your password: ']
print[password]
4

Giờ đây, bạn có thể sử dụng phương thức

import getpass

password = getpass.getpass['Enter your password: ']
print[password]
3 để hiển thị dấu hoa thị khi người dùng nhập mật khẩu của họ

import pwinput

password = pwinput.pwinput[prompt='Enter your password: ', mask='*']
print[password]

Đối số

import getpass

password = getpass.getpass['Enter your password: ']
print[password]
6 là thông báo được hiển thị và đối số
import getpass

password = getpass.getpass['Enter your password: ']
print[password]
2 là những gì để che dấu đầu vào của người dùng bằng

Nếu bạn chỉ muốn ẩn đầu vào trong khi người dùng nhập mật khẩu của họ, hãy sử dụng mô-đun

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 tiêu chuẩn

import getpass

password = getpass.getpass['Enter your password: ']
print[password]

Phương pháp từ mô-đun

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 được sử dụng để ẩn đầu vào trong khi người dùng nhập mật khẩu của họ

Mô-đun

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 có sẵn trong thư viện tiêu chuẩn, vì vậy bạn không phải cài đặt bất cứ thứ gì

Phương pháp

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 thường được sử dụng để nhắc người dùng nhập mật khẩu hoặc thông tin nhạy cảm khác

Đối số mà phương thức nhận là thông báo được hiển thị cho người dùng

Nếu không có thông báo nào được cung cấp, nó sẽ mặc định là "Mật khẩu. "

import getpass

password = getpass.getpass[]
print[password]

Chúng tôi đã không chuyển một tin nhắn đến phương thức

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
0 nên nó hiển thị mặc định "Mật khẩu. " thông điệp

Bạn có thể sử dụng phương pháp

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
0 để lấy bất kỳ thông tin nhạy cảm nào khác từ người dùng mà không cần hiển thị văn bản, không cần phải nhập mật khẩu

Nhắc tên người dùng và mật khẩu trong Python

Để nhắc người dùng nhập tên người dùng và mật khẩu

  1. Sử dụng hàm input[] để lấy các giá trị đầu vào không nhạy cảm, chẳng hạn như tên người dùng
  2. Sử dụng phương pháp
    import getpass
    
    
    attemps = 0
    
    while attemps < 3:
        username = input['Enter your username: ']
        password = getpass.getpass['Enter your password: ']
    
        if username == 'user123' and password == 'password123':
            print['You have successfully logged in.']
            break
        else:
            print['Incorrect credentials. Check if you have Caps lock on and try again.']
            attemps += 1
            continue
    
    0 để lấy các giá trị nhạy cảm, chẳng hạn như mật khẩu

Ví dụ đầu tiên sử dụng hàm input[] để lấy tên người dùng và mật khẩu của người dùng

Hàm input[] hiển thị văn bản khi người dùng đang nhập

Hàm nhận một đối số tùy chọn

import getpass

password = getpass.getpass['Enter your password: ']
print[password]
6 và ghi nó vào đầu ra tiêu chuẩn mà không có một dòng mới ở cuối

Sau đó, hàm đọc dòng từ đầu vào, chuyển đổi nó thành chuỗi và trả về kết quả

Lưu ý rằng hàm input[] được đảm bảo trả về một chuỗi ngay cả khi người dùng nhập một số

Ví dụ thứ hai sử dụng phương thức

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 để lấy tên người dùng và mật khẩu mà không hiển thị văn bản khi người dùng đang nhập

import getpass


username = getpass.getpass['Enter your username: ']
print[username]

password = getpass.getpass['Enter your password: ']
print[password]

Chúng tôi đã sử dụng phương pháp từ mô-đun

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 để nhắc người dùng nhập dữ liệu mà không lặp lại

Mô-đun

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 có sẵn trong thư viện tiêu chuẩn, vì vậy bạn không phải cài đặt bất cứ thứ gì

Phương pháp

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
1 thường được sử dụng để nhắc người dùng nhập mật khẩu hoặc thông tin nhạy cảm khác

Đối số mà phương thức nhận là thông báo được hiển thị cho người dùng

Bạn cũng có thể kết hợp các giải pháp và sử dụng hàm input[] để lấy tên người dùng và phương thức

import getpass


attemps = 0

while attemps < 3:
    username = input['Enter your username: ']
    password = getpass.getpass['Enter your password: ']

    if username == 'user123' and password == 'password123':
        print['You have successfully logged in.']
        break
    else:
        print['Incorrect credentials. Check if you have Caps lock on and try again.']
        attemps += 1
        continue
0 để lấy mật khẩu

Làm cách nào để tạo tên người dùng trong Python?

Nhận tên người dùng và mật khẩu khi chạy bằng Python .
pip cài đặt getpass. con trăn. Sao chép
tên người dùng = getpass. getuser[] Python. Sao chép
userName= input['Nhập tên người dùng. '] Con trăn. Sao chép
mật khẩu = getpass. getpass[] Python. Sao chép

Làm cách nào để tạo thông tin đăng nhập bằng Python?

Tìm hiểu từng bước .
Tạo cửa sổ menu chính
Tạo cửa sổ đăng ký
Đăng ký thông tin của người dùng trong tệp văn bản bằng Python
Kiểm tra xem thông tin của người dùng đã tồn tại hay chưa
Tạo cửa sổ đăng nhập và xác minh người dùng

Chủ Đề