menator01
Gold Coder
Been a while since posting so, I though I would get a number guessing game started and see if anyone wants to add to it. Change re-arrange or whatever needed.
I've done several just thought I would try and get some kind of community group thing going. Sorry if I've posted this under the wrong topic.
Modules needed is pandas and tabulate. Both can be gotten using pip
I've done several just thought I would try and get some kind of community group thing going. Sorry if I've posted this under the wrong topic.
Modules needed is pandas and tabulate. Both can be gotten using pip
Python:
# Do the imports
from pandas import DataFrame as df
from tabulate import tabulate as tb
# Create Player class
class Player:
'''
Player class contains a name and scores for win/loose/tie
'''
def __init__(self, name='Computer'):
self.stats = {
'name': name.title(),
'wins': 0,
'losses': 0,
'ties': 0
}
# Create winner class
class Winner:
'''
Winner class does our comparison to see who wins
'''
def __init__(self, number):
self.number = number
def __lt__(self, other):
return self.number < other.number
def __gt__(self, other):
return self.number > other.number
def __eq__(self, other):
return self.number == other.number
if __name__ == '__main__':
# Using while loop to get a player name and some error checking
while True:
print('Enter player name:')
name = input('>> ')
# If statement to make sure only letters are used for name
# Sets up our player and computer player
# Else is some basic error checking
if name.strip().isalpha():
player = Player(name)
player2 = Player()
break
else:
print('You must enter a name.\nNames can only contain letters.')
# Start the game loop
while True:
# A way to exit the game
print('Enter \'q\' to quit')
data = [player.stats, player2.stats]
# Print out player and computer player data
print(tb(df(data), tablefmt='grid', headers=[dat.title() for dat in data[0].keys()], showindex=False, numalign='center'))
# Prompt for getting user input
user_input = input('>> ').lower()
# q was entered exit the game
if user_input == 'q':
print('Goodbye!')
break
# Play the game
else:
# Try block for checking only whole numbers are entered
try:
number = int(user_input)
except ValueError:
user_input = 'blank space' if user_input.strip() == '' else user_input
print(f'{user_input} character is not allowed. Only whole number can be entered.')