Hack Forums
[Python] Dice Class - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Dice Class (/showthread.php?tid=5829412)



Dice Class - Mr.Richie - Apr 25, 2018

Saw tutorial before.. now it's gone. Tried this, it's not working.

Code
import random

class Player:
    def __init__(self, name):
    self.name = name
    self.dice1 = random.randint(1,6)
    self.dice2 = random.randint(1,6)
    self.total = dice1 + dice2

    def get_dice(self):
    print("Player: {0}, dice1: {1}, dice2: {2}, total scored: {3}".format(self.name, self.dice1, self.dice2, self.total)

player1 = Player("John")
player2 = Player("Jake")

player1.get_dice()
player2.get_dice()



RE: Dice Class - .kr. - Apr 25, 2018

Post the error message here please


RE: Dice Class - Mr.Richie - Apr 25, 2018

(Apr 25, 2018 09:44 AM).kr. Wrote:  Post the error message here please

Line 5 error self.name = name.


RE: Dice Class - HFDone - Apr 25, 2018

theres few errors in your code dice1 + dice2 and print close
PHP Code

<?php 
class Player:
 
  def __init__(self, name):
 
     self.name = name
      self
.dice1 = random.randint(1,6)
 
     self.dice2 = random.randint(1,6)
 
     self.total = self.dice1 + self.dice2
    
   def get_dice
(self):
 
     print "Player: {0}, dice1: {1}, dice2: {2}, total scored: {3}".format(self.name, self.dice1, self.dice2, self.total)
 
      
p1 
= Player("John")
p2 = Player("Jake")

p1.get_dice()
p2.get_dice() 



RE: Dice Class - .kr. - Apr 25, 2018

some error's in there... somtimes no 'self', simetimes no ')' and somtimes indent not right

Code
import random

class Player:
    def __init__(self, name):
        self.name = name
        self.dice1 = random.randint(1,6)
        self.dice2 = random.randint(1,6)
        self.total = self.dice1 + self.dice2

    def get_dice(self):
        print("Player: {0}, dice1: {1}, dice2: {2}, total scored: {3}".format(self.name, self.dice1, self.dice2, self.total))


player1 = Player("John")
player2 = Player("Jake")

player1.get_dice()
player2.get_dice()



RE: Dice Class - cleptom - Apr 25, 2018

(Apr 25, 2018 11:35 AM).kr. Wrote:  some error's in there... somtimes no 'self', simetimes  no ')' and somtimes indent not right

Code
import random

class Player:
   def __init__(self, name):
   self.name = name
   self.dice1 = random.randint(1,6)
   self.dice2 = random.randint(1,6)
   self.total = self.dice1 + self.dice2

   def get_dice(self):
    print("Player: {0}, dice1: {1}, dice2: {2}, total scored: {3}".format(self.name, self.dice1, self.dice2, self.total))


player1 = Player("John")
player2 = Player("Jake")

player1.get_dice()
player2.get_dice()

If you ever need to import this class (for an assignment building off of this or something similar) it will run your test code during the import. To prevent this, you can do something like this:
Code
import random

class Player:
    def __init__(self, name):
    self.name = name
    self.dice1 = random.randint(1,6)
    self.dice2 = random.randint(1,6)
    self.total = self.dice1 + self.dice2

    def get_dice(self):
     print("Player: {0}, dice1: {1}, dice2: {2}, total scored: {3}".format(self.name, self.dice1, self.dice2, self.total))


if __name__ == "__main__":
    player1 = Player("John")
    player2 = Player("Jake")

    player1.get_dice()
    player2.get_dice()

This way your test cases will only run if your code is explicitly ran (ie python class.py)