I have just gotten into Object Oriented Programming and I've been practicing it through Python. But I think I still haven't clearly grasped how it should be used effectively. I've created a game Using OOP where a random deck is generated and will be compared to a random number given to an NPC. Below are the source code:
import random
import os
class User:
def __init__(self, username, NumList):
self = NPC()
self.username = username
self.NumList = NumList
print(f'Welcome {self.username}! We have initialized your card!')
print(f'Your numbers are: {self.NumList}')
User.userSystem(self)
def userSystem(self):
print("")
print(f'So, What would you like to do now, {self.username}?')
print(f'Type (1) to Reroll your card')
print(f'Type (2) to bet')
userInput = int(input())
if userInput == 1:
Deck.reRoll(self)
User.userSystem(self)
elif userInput == 2:
Deck.bet(self)
else:
print("Invalid Argument! Please type a valid number!")
print("")
return User.userSystem(self)
class Deck(User):
def __init__(self, username):
self.NumList = []
for x in range(5):
self.NumList.append(random.randrange(0,100))
User.__init__(self, username, self.NumList)
def reRoll(self):
print(f'Re-rolling your Numbers!')
self.NumList = []
for x in range(5):
self.NumList.append(random.randrange(0,100))
print('Numbers Re-rolled!')
print(f'Your new numbers are: {self.NumList}')
def bet(self):
print("")
myCard = self.NumList
print(f'Your cards are: {myCard}')
print(f'The value is: {sum(myCard)}')
print("")
opponentCard = NPC.callNumbers(self)
print(f'Your opponents cards are: {opponentCard}')
print(f'Their value is: {sum(opponentCard)}')
print("")
if sum(myCard) > sum(opponentCard):
print(f'Congratulations {self.username}! You Won!')
elif sum(myCard) < sum(opponentCard):
print(f'You lost the round, {self.username}!')
else:
print(f'It is a draw!')
class NPC(User):
def __init__(self):
self.username = f'Player {random.randrange(1,1000)}'
self.numlist = []
for x in range(5):
self.numlist.append(random.randrange(0,100))
def callNumbers(self):
return self.numlist
#Initiate
user = input("Welcome! Please type your name: ")
os.system('CLS')
user = Deck(user)
I would love to know if this is how Object Oriented Programming supposed to be used, or in any case perhaps I am making a mistake or any unnecessary steps.