I have two problems with understanding the concept of key and value in text file using python.
The first one:
I have this file which called login.txt
has the several keys and values such as:
{'ID':1, 'Username':'x', 'Password':'123','Role':'Immigration officer'}
{'ID':2, 'Username':'y', 'Password':'321','Role':'Customs officer'}
And here is my login code, it's working but it still looping each time I login with the username and password. But I need it once I logged in with any right values in the txt file to stop the loop directly, I don't know if I'm understand the concept or not. Here is the code:
def login(userinput, passinput):
with open(login_db, 'r') as f:
for single_line in f:
dict_to_check = ast.literal_eval(single_line)
username = dict_to_check['Username']
password = dict_to_check['Password']
role = dict_to_check['Role']
try:
if userinput == username:
if passinput == password:
if role == 'Immigration officer':
Immigration()
elif role == 'Customs officer':
Customs()
else:
print('Incorrect role you do not have the authority.')
else:
print('Password is incorrect.') #It still looping here
else:
print('Incorrect username')
except:
print("An exception occurred")
The second problem is: I have another txt file called passenger.txt
which have the following content:
{'Civil ID':'123', 'Name':'John', 'DOB':'12/12/2000', 'Gender':'Male', 'Customs fine':'', 'Status':''}
{'Civil ID':'1010', 'Name':'Sara', 'DOB':'6/10/2000', 'Gender':'Female', 'Customs fine':'', 'Status':''}
I have a code to choose a specific column with the ID after that I want to modify the Status
value of this ID. Here is the code:
def Existing_passenger(ID):
with open(passenger_db, 'r') as f:
for single_line in f:
dict_to_check = ast.literal_eval(single_line)
civil_id = dict_to_check['Civil ID']
name = dict_to_check['Name']
dob = dict_to_check['DOB']
gender = dict_to_check['Gender']
customs_fine = dict_to_check['Customs fine']
status = dict_to_check['Status']
try:
if ID == civil_id:
print("-Passenger's information-")
print('Name: '+name+'\nDate of birth: '+dob+'\nGender: '+gender+'\nCustoms fine: '+customs_fine+'\nStatus: '+status+'\n')
print('A. Arrival Approved\nB. Arrival Rejected\nC. Departure Approved\nD. Departure Rejected\nE. Go Back to previous menu')
status_input = input('Select one of the following options: ')
if status_input == 'A':
if status == '':
#What should i write here to modify its status value??
elif status_input == 'B':
print('B')
elif status_input == 'C':
print('C')
elif status_input == 'D':
print('D')
elif status_input == 'E':
print('E')
else:
print('Wrong entry, please try again.')
except:
print("An exception occurred")
I try to understand the right concept of the key and value pairs using text file with python programming language.
First one:
To stop the loop once a match is found, you should add a break
statement after the correct login is found. For example:
...
if userinput == username:
if passinput == password:
if role == 'Immigration officer':
Immigration()
elif role == 'Customs officer':
Customs()
else:
print('Incorrect role you do not have the authority.')
break # stop the loop
else:
print('Password is incorrect.')
else:
print('Incorrect username')
Second one:
If the original value of passenger['Status']
is ''
, and you write passenger['Status'] = 'Arrival Approved'
, then the value of passenger['Status']
will be updated to 'Arrival Approved'
.
...
if status_input == 'A':
if status == '':
status = 'Arrival Approved' # modify the status value
...