Dec-14-2020, 03:56 AM
(This post was last modified: Dec-14-2020, 04:14 AM by LastStopDEVS.
Edit Reason: Cleaned up the code separation a bit to make it easier to read.
)
First off, I appreciate anyone who takes a look at this code. I did my best to read the rules here on the forums and I am trying my best to follow them. I am a new user here so please pardon me if I make a mistake and please don't hesitate to let me know how I can improve as a forum member. I would hope to become a contributing member here on the forums myself now that I am pretty familiar with the python language and I would also like to really start rounding out my overall knowledge.
Secondly, this is just a junk script that I have coded to improve my overall OOP abilities for RPG programming. I know there are privative methods here and probably things I am doing completely wrong and that is okay, don't worry about that. Any pointers to proper documentation I should be reading is always appreciated as there is so much vast information to absorb it is always nice to have suggestions and go back to the references I may have missed or forgotten. The main objective here is to help me understand what I am missing on the current level I am at. I assumed I knew enough of this language to read every line and find this out, I have done some searching and I cant quite find a post that matches. Maybe I just need a fresh pair of eyes on here and it's super obvious, we will see. I only managed to discover what the output was referenced to and not where or why it is being printed in my output.
_________________________
The Question: Why does the Account.id get printed/returned to the very end of my console output after player is created and login is successful?
Secondly, this is just a junk script that I have coded to improve my overall OOP abilities for RPG programming. I know there are privative methods here and probably things I am doing completely wrong and that is okay, don't worry about that. Any pointers to proper documentation I should be reading is always appreciated as there is so much vast information to absorb it is always nice to have suggestions and go back to the references I may have missed or forgotten. The main objective here is to help me understand what I am missing on the current level I am at. I assumed I knew enough of this language to read every line and find this out, I have done some searching and I cant quite find a post that matches. Maybe I just need a fresh pair of eyes on here and it's super obvious, we will see. I only managed to discover what the output was referenced to and not where or why it is being printed in my output.
_________________________
The Question: Why does the Account.id get printed/returned to the very end of my console output after player is created and login is successful?
Output:[Account Creation Menu]
Username: Guest
Password: 123
[Account Information & Details]
User ID: 1
Username: Guest
Password: (Hidden)
VIP Rank: None
[Updating Table Entry]
{'ID': 1, 'Username': 'Guest', 'Password': '***', 'Rank': None}
--Table Entry Updated Successfully!--
[.:: Please Login ::.]
Username: Guest
Password: 123
--[Logging Into System]--
Login Successful...
1import time
import random
import json
cache = None
class Account:
Accounts = {'ID': None,
'Username': None,
'Password': None,
'Rank': None}
i = 0
def __init__(self,
id,
username,
password,
rank):
self.id = id
self.username = username
self.password = password
self.rank = rank
self.__str__()
def __str__(self):
return ('\n\n[Account Information & Details]\n\nUser ID: {}\nUsername: {}\nPassword: (Hidden)\nVIP Rank: {}'.format(self.id,
self.username,
self.rank))
def create_new(self, amt):
while Account.i < amt:
global cache
Account.i += 1
print('\n[Account Creation Menu]\n')
account = Account(Account.i,
input('\nUsername: '),
input('Password: '),
None)
cache = [account.id,
account.username,
account.password,
account.rank]
Account.Accounts.update({'ID': account.id,
'Username': account.username,
'Password': '*'*len(account.password),
'Rank': account.rank})
print(account,'\n\n[Updating Table Entry]\n',Account.Accounts)
self.write_file()
print('--Table Entry Updated Successfully!--')
print('\n\n[.:: Please Login ::.]\n')
account.login(input('Username: '),
input('Password: '))
def login(self,
username,
password):
global cache
print('\n--[Logging Into System]--\n')
time.sleep(2)
if username == cache[1] and password == cache[2]:
print('Login Successful...')
del cache[2]
else:
print('\nInvalid Login, please try again!\n')
self.login(input('Username: '),
input('Password: '))
def write_file():
with open('Accounts_Table.txt', 'a') as file: #change 'w' to 'a' to solve the file overwriting issue.
file.write('\r'+json.dumps(Account.Accounts))
class Player():
def __init__(self, name):
self.name = name
self.inventory = [Currency('Gold', 1)]
self.hp = int(random.randrange(250, 500))
def __str__(self):
print('\nPlayer Name: {}\nHealth: {}\n'.format(self.name, self.hp, self.print_inventory))
def print_inventory(self):
print(self)
print('Here is a list of your items:\n')
for item in self.inventory:
print(item,'\n')
class Item():
currency = ['Gold']
def __init__(self,
name,
description,
value):
print(cache[0])
self.name = name
self.description = description
self.value = value
def __str__(self):
return('Name: {}\nDescription: {}\nValue: {}\n'.format(self.name,
self.description,
self.value))
class Container(Item):
def __init__(self,
name,
description,
value,
contents,
slots_left,
max_slots):
self.contents = contents
self.slots_left = slots_left
self.max_slots = max_slots
super().__init__(name, description, value)
def __str__(self):
return('Name: {}\nDescription: {}\nValue: {}\n\n[Contents: {}]\nAvailable Slots: {}/{}'.format(self.name,
self.description,
self.value,
self.contents,
self.slots_left,
self.max_slots))
class Currency(Item):
def __init__(self, ctype, val):
self.ctype = ctype
self.val = val
if ctype.capitalize() in Item.currency:
if ctype.capitalize() == Item.currency[0]:
super().__init__(name = ctype.capitalize(),
description = 'A shiny gold coin. It appears to be expensive',
value = val)
elif ctype not in Item.currency:
print('|Invalid| - Item \'{}\' is not a valid currency item!'.format(ctype))
else:
print('|Unhandled Exception| Critical Error - Contact Developer!')
def __str__(self):
return('Currency Type: {}\nDescription: {}\nValue: {}'.format(self.ctype,
self.description,
self.val))
while __name__ == '__main__':
Account.create_new(Account, 1)
player = Player(cache[1])
breakThank you again to anyone who offers some advice or any help. I wish you a very Merry Christmas and happy holidays throughout this upcoming New Year!
