Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Match coins game
#2
Change method set_amount,then it should work:
def set_amount (self, value):
    self.__amount += value
The  getters/setters and __(which is not private),could be removed.
Double underscores __ is used for name mangling,nothing is really private in Python.
Not your fault but your teacher who comes from a Java/C++ background,
and do it in a same as those languages in Python.
So could look like this:
import random

class Coin:
    def __init__ (self, amount=20, sideup="Heads"):
        self.sideup = sideup
        self.amount = amount

    def toss(self):
        val = random.randint(0,1)
        if val == 0:
            self.sideup = "Heads"
        else:
            self.sideup = "Tails"

c1 = Coin()
c2 = Coin()
play = input('Do you want to play? (Y/N): ')
while play.lower() == 'y':
    c1.toss()
    c2.toss()
    if c1.sideup == c2.sideup:
        c1.amount += 5
        c2.amount -= 5
        print("player 1 wins")
    else:
         c1.amount -= 5
         c2.amount += 5
         print("player 2 wins")
    print(f'player1: {c1.amount} and player2: {c2.amount}')
    play = input("Do you want to play? (y/n): ")
Reply


Messages In This Thread
Match coins game - by AndJustice4A11x - Nov-04-2017, 08:38 PM
RE: Match coins game - by snippsat - Nov-05-2017, 01:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding and Removing coins to match Coin Bag Total infinite times Strayfe 8 4,871 Sep-11-2018, 07:30 PM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020