This program basically asks you for a username and a password, which is in the code, in python. There are no errors with my code, but there are two things I've been trying to solve that I just can't think of as a beginner.
First off is when you type in anything that isn't the username or password, it'll spit out that the username or password is wrong, and it'll show the the amount of tries left, which are 3. If you keep getting the username and/or password wrong, it'll start with saying that you have 0 attempts and keep going up until 2, where they program will end.
I'm not to sure but I think the problem maybe in the for
loop. How can I get it to start with it saying you have 3 attempts going down to 1?
The second thing I've been trying to do is making my code less repetitive. I'm new to classes and objects and I'm trying to make the last two functions do the same thing as the other functions, specifying the users input, such as while choice1 == self.username and choice2 != self.password:
, and without having to type in all of the code.
Here is the code.
from sys import exit
guesses = 3
terms = {
'incorrect': 'Incorrect Username or Password!',
'end_guess': 'Your session has been terminated',
'welcome': 'Welcome Sys Admin'
}
choice1 = input("Username> ")
choice2 = input("Password> ")
class Options:
def __init__(self, username, password):
self.username = username
self.password = password
def correct(self):
if choice1 == self.username and choice2 == self.password:
print(terms['welcome'])
def incorrect1(self):
global guesses
global choice1
global choice2
while choice1 != self.username and choice2 != self.password:
for i in range(guesses):
print(f"{terms['incorrect']}\n{i} attempts left")
choice1 = input("Username> ")
choice2 = input("Password> ")
guesses-= 1
if choice1 == self.username and choice2 == self.password:
print(terms['welcome'])
exit(0)
else:
print(terms['end_guess'])
exit(0)
def incorrect2(self):
global guesses
global choice1
global choice2
while choice1 != self.username and choice2 == self.password:
for i in range(guesses):
print(f"{terms['incorrect']}\n{i} attempts left")
choice1 = input("Username> ")
choice2 = input("Password> ")
guesses -= 1
if choice1 == self.username and choice2 == self.password:
print(terms['welcome'])
exit(0)
else:
print(terms['end_guess'])
exit(0)
def incorrect3(self):
global guesses
global choice1
global choice2
while choice1 == self.username and choice2 != self.password:
for i in range(guesses):
print(f"{terms['incorrect']}\n{i} attempts left")
choice1 = input("Username>")
choice2 = input("Password> ")
guesses -= 1
if choice1 == self.username and choice2 == self.password:
print(terms['welcome'])
exit(0)
else:
print(terms['end_guess'])
exit(0)
# defining the username as admin and password as helloworld
final = Options("admin", "helloworld")
final.correct()
final.incorrect1()
final.incorrect2()
final.incorrect3()