Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Mark Libario
9,003 PointsHello guys! What do you think about how I write code? Anything I need to improve?
I am practicing what I learned and so far Im in OOP classes. I want to know if my code is "readable" and what do i need to keep an eye on or to improve in terms of writing code..
main.py
from random import randint
from config import Dice, Player
def craps():
player_name = input("What is your name?: \n"
">> ")
player_cash = int(input("How much cash are you bringing in? \n"
">> "))
player1 = Player(player_name,player_cash)
print()
print("Welcome {}. You are currently starting with ${}".format(player1.name, player1.cash))
print("Let us play the famous game of CRAPS! \n"
"Here are the rules: \n \n"
"1. Each round in craps starts with what is known as the come-out roll.\n"
"Most casinos will require that the shooter place a bet before making this roll on either the pass line or the don’t pass line. \n"
"2. If the combined value of the two dice on a come-out roll equals 2, 3, or 12, then this is “crapping out” and the round is over. \n"
"Players will lose pass line bets and win don’t pass bets. If the come-out roll is 7 or 11, the round is also over. \n"
"Let's start by putting a bet before the come-out roll.\n"
"\n"
"Place your bets! \n")
while True:
player_bet = int( input("Please input the amount of money you want to bet: \n >>"))
if player_cash < player_bet:
print("Sorry, but it seems that you don't have sufficient cash available.")
continue
else:
player_cash -= player_bet
break
come_out_bet = player_bet
while True:
come_out_roll = input("Please input 'PASS' for the Pass line and 'DPASS' for Don't Pass line:\n"
">> ")
if come_out_roll.upper() == "PASS":
print("{} bets ${} on pass the pass line!".format(player1.name,come_out_bet))
break
elif come_out_roll.upper() == "DPASS":
print("Player bets ${} on pass the Don't pass line!".format(come_out_bet))
break
else:
print("Answer not recognized, please try again!")
continue
user_input = input("Type 'ROLL' to start your throw! \n"
">> ")
if "ROLL" in user_input.upper():
player1.RollDice()
print("{} rolled a {}!".format(player1.name,player1.RollDice()))
user_input = []
user_input = input("Are you ready to play? y/n \n"
">> ")
while True:
if "Y" in user_input.upper():
craps()
break
else:
print("Goodbye!")
break
config.py
from random import randint
class Dice:
def __init__(self, sides):
self.sides = sides
def roll_die(self):
return randint(1, self.sides)
class Player:
"""The Player class creates a new player with a name argument and a cash argument. The cash argument is sued to know how much money the player has."""
def __init__(self, name, cash):
self.name = name
self.cash = cash
def RollDice(self):
"""I want the player to be able to use the class Dice method"""
dice1.roll_die()
dice2.roll_die()
return dice1.roll_die() + dice2.roll_die()
dice1 = Dice(6)
dice2 = Dice(6)