Guest User

Example of while loop

a guest
Nov 24th, 2017
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. '''Example of a while loop
  4. Only 1 user: 'mark' with password 'spoon'
  5. '''
  6. def rec_pass(password, tries):
  7.     '''checks password for a number of tries'''
  8.     if password == 'spoon':
  9.         return 1
  10.     elif tries == 1:
  11.         return 0
  12.     else:
  13.         return rec_pass(input('Enter Password: '), tries-1)
  14.  
  15. def _main():
  16.     exit_flag = 1
  17.     while exit_flag == 1:
  18.  
  19.         username = input('Enter username: ')
  20.         if username == 'mark':
  21.             print('O Hai! {}!'.format(username))
  22.         else:
  23.             print('Unknown User!')
  24.             continue
  25.  
  26.         access = rec_pass(input('Enter Password: '), 3)
  27.         if access == 1:
  28.             print('Access Granted')
  29.             break
  30.         else:
  31.             print('Max retries reached')
  32.             exit_flag = 0
  33.  
  34.     else:
  35.         print('Invalid Password! Program Terminated')
  36.  
  37. if __name__ == '__main__':
  38.     _main()
Add Comment
Please, Sign In to add comment