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()
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)
|