Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. db = dict()
  2. #Empty list that acts as a database
  3.  
  4. state = False
  5. #State defined to start and stop while loop which acts as our on off switch for the database
  6.  
  7. def get_user():
  8. user1 = input("What is your username?\n")
  9. user = user1
  10. return user
  11.  
  12. def get_pass():
  13. password = input("What is your password?\n")
  14. return password
  15. def login():
  16. #This is going to be called when user says he is logging in.
  17. #user and pass variables used to check in db for existing usernames
  18. user = get_user()
  19. password = get_pass()
  20.  
  21. #if checks through every object in database to see if user and pass are in db
  22. if user in db and password == db[user][0]:
  23. print("Hello ", user,"\n")
  24. loginchoice(user)
  25. return user
  26.  
  27. #Else catches if username and pass fail and call back decision function
  28. else:
  29. print("Error: Username or Password incorrect.\n")
  30. userinput()
  31.  
  32. def signup():
  33. #This is called when user chooses signing up
  34. #user and pass variables used to check in db for existing usernames
  35. user = input("What is your username?\n")
  36. password = input("What is your password?\n")
  37.  
  38. #Boolean tree used if signup username is original and adds the input into database
  39. if user not in db:
  40. db[user] = [password, 0]
  41. print("Sign up complete. Please attempt to login. \n")
  42. userinput()
  43. #If boolean check fails it means there is identical username in database and it cannot add it
  44. else:
  45. print("This username already exists, please try again.\n")
  46. userinput()
  47.  
  48. def test():
  49. #This function prints out the db to make sure we are adding users and passwords to database
  50. print(db)
  51. userinput()
  52.  
  53. def userinput():
  54. #State activates while loop and "turns on" our database
  55. state = True
  56.  
  57. #This function asks the user on whether they want to login or signup
  58. #Because it is setup this way it gives the user the ability to choose from several options.
  59. question = input("Press G to login, Press S to signup, Press T for test, or press Q to quit.\n")
  60.  
  61. if question.lower() == 'g':
  62. login()
  63. elif question.lower() == 's':
  64. signup()
  65. elif question.lower() == 't':
  66. test()
  67. elif question.lower() == 'q':
  68. state = False
  69. print("Thanks for your time! Login soon.\n")
  70. else:
  71. print("ERROR: Invalid input\n")
  72. userinput()
  73.  
  74. def check_balance(user):
  75. print("Your balance is:", db[user][1])
  76. loginchoice(user)
  77.  
  78. def deposit(user):
  79. print(user)
  80. deposit = input("How much would you like to deposit?\n")
  81. db[user][1] += int(deposit)
  82. print("Your new balance is now:", db[user][1])
  83. loginchoice(user)
  84.  
  85. def withdraw(user):
  86. withdraw = input("How much would you like to withdraw?\n")
  87. db[user][1] -= int(withdraw)
  88. print("Your new balance is now:", db[user][1])
  89. loginchoice(user)
  90.  
  91. def loginchoice(user):
  92. #This function is called when login is successful.
  93. #It gives a further decision tree for logged in users.
  94. choice = input("What would you like to do?\n1.) Check balance. \n2.) Deposit money \n3.) Withdraw money \n4.) Back to menu. \n")
  95. if int(choice) == 1:
  96. check_balance(user)
  97. elif int(choice) == 2:
  98. deposit(user)
  99. elif int(choice) == 3:
  100. withdraw(user)
  101. elif int(choice) == 4:
  102. userinput()
  103. else:
  104. print("ERROR: Invalid input\n")
  105. loginchoice(user)
  106.  
  107.  
  108. def game_start():
  109. userinput()
  110.  
  111. while state == True:
  112. #While loop initiates database start and calls functions when needed.
  113. user = get_user()
  114.  
  115.  
  116. game_start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement