Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Dice Class

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()
Post the error message here please
(Apr 25, 2018 09:44 AM).kr. Wrote:  Post the error message here please

Line 5 error self.name = name.
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() 
[Image: bitcoin.png] Be Nice For Once 15hVUVz7xA4kazHdk2BuErENGCskVwjYXP
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()
(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)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by lockheadApr 22, 2021 07:10 AM 0 146 Apr 22, 2021 07:10 AM
Last Post: lockhead
[Python] by ICUPApr 3, 2020 08:21 AM 3 183 Apr 3, 2020 08:21 AM
Last Post: lockhead
[Python] by Will PattersonOct 6, 2017 11:02 AM 9 289 Oct 6, 2017 11:02 AM
Last Post: koolkid