Tôi chỉ là một lập trình viên mới bắt đầu ngay bây giờ và tôi đang cố gắng tạo một chương trình tên người dùng/mật khẩu. Đây là mã của tôi dưới đây: username = 'Polly1220'
password = 'Bob'
userInput = input("What is your username?\n")
if userInput == username:
a=input("Password?\n")
if a == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
Bất cứ khi nào tôi chạy chương trình của mình và nhập mật khẩu chính xác như được hiển thị, nó luôn nói rằng mật khẩu không chính xác.
Đã hỏi ngày 20 tháng 6 năm 2017 lúc 21:33Jun 20, 2017 at 21:33 6 Bạn cần gán câu lệnh đầu vào thứ hai cho UserInput: if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
Đã trả lời ngày 20 tháng 6 năm 2017 lúc 21:35Jun 20, 2017 at 21:35 4 Bạn quên gán biến: if userInput == username
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
Đã trả lời ngày 20 tháng 6 năm 2017 lúc 21:36Jun 20, 2017 at 21:36 Damian Lattenerodamian LatteneroDamian Lattenero 15,5k3 huy hiệu vàng41 Huy hiệu bạc72 Huy hiệu đồng3 gold badges41 silver badges72 bronze badges Tên người dùng và đầu vào mật khẩu với 3 lần thử trong Python #Để lấy các giá trị đầu vào của người dùng và mật khẩu với 3 lần thử: - Sử dụng vòng lặp
while để lặp lại tối đa 3 lần. - Sử dụng hàm
input() để lấy các giá trị cho tên người dùng và mật khẩu từ người dùng. - Nếu thông tin đăng nhập là chính xác, thoát ra khỏi vòng lặp.
Copied! attemps = 0
while attemps < 3:
username = input('Enter your username: ')
password = input('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 vòng lặp while để lặp lại tối đa 3 lần. Trên mỗi lần lặp, chúng tôi nhắc người dùng cho tên người dùng và mật khẩu và kiểm tra xem các giá trị có chính xác không. Nếu bạn muốn ẩn văn bản mật khẩu trong khi người dùng đang gõ, hãy sử dụng phương thức getpass() . Copied! 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 thức getPass () từ mô -đun getpass để nhắc người dùng về mật khẩu mà không lặp lại. Mô -đun getpass 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ứ điều gì. Phương thức getpass thường được sử dụng để nhắc người dùng về 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 if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
2 đánh giá thành if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
3, khối if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
2 chạy ở nơi chúng ta thoát ra khỏi vòng lặp while .Tuyên bố phá vỡ thoát ra khỏi vòng lặp if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
6 hoặc while .Nếu một hoặc cả hai điều kiện đánh giá thành if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
8, khối if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
9 sẽ chạy.Trong khối if userInput == username:
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
9, chúng tôi tăng biến if userInput == username
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
1 bằng if userInput == username
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
2 và tiếp tục lần lặp tiếp theo của vòng lặp while . Tuyên bố if userInput == username
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
4 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 thông tin đăng nhập không chính xác 3 lần, biến if userInput == username
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
1 sẽ được đặt thành if userInput == username
userInput = input("Password?\n")
if userInput == password:
print("Welcome!")
else:
print("That is the wrong password.")
else:
print("That is the wrong username.")
6 và điều kiện trong vòng lặp while không còn được đáp ứng.
Làm thế nào để Python lưu trữ tên người dùng và mật khẩu?
Bạn có thể lưu trữ tên người dùng/mật khẩu trên hai dòng đầu tiên của tệp văn bản đơn giản, sau đó sử dụng Python để đọc nó khi bạn cần.Nếu tệp văn bản nằm trong thư mục kho lưu trữ, bạn nên sửa đổi.Gitignore để đảm bảo nó không được theo dõi bởi điều khiển nguồn.on the first two lines of a plain text file, then use python to read it when you need it. If the text file is in the repository directory you should modify . gitignore to ensure it's not tracked by source source control.
Làm thế nào để Python xác nhận tên người dùng và mật khẩu?
Làm thế nào để Python xác nhận tên người dùng và mật khẩu ?.. Nhập mô -đun RE Python .. Chạy một vòng vô hạn .. Yêu cầu người dùng nhập một mật khẩu .. Kiểm tra xem độ dài của mật khẩu có từ 6 đến 12 hay không .. Kiểm tra xem mật khẩu có chứa bất kỳ ký tự chữ hoa nào hay không, nếu không in một tin nhắn và tiếp tục bắt đầu vòng lặp .. |