Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple cards game
#1
Please help, I'm new to python and I've been trying to figure this out for HOURS.
I must create a program that follows these rules:

1. The game starts with certain initial amount of dollars.
2. At each round of the game, instead of flipping a coin, the player shuffles a deck and draws 6 cards. If the drawn hand contains at least one ace, the player gains a dollar, otherwise they lose a dollar.
3. The game runs until the player either runs out of money or doubles their initial amount.

To test the game, given the initial amount, run it 1000 times to determine how many rounds does the game last on average.

This is my code:

import random

faceValues = ['ace', '2', '3', '4', '5', '6',
              '7', '8', '9', '10', 'jack',
              'queen', 'king']

suits = ['clubs', 'diamonds', 'hearts',
         'spades']

def shuffledDeck():
    deck = []
    for faceValue in faceValues:
        for suit in suits:
            deck.append(faceValue + ' of ' + suit)
    random.shuffle(deck)
    return deck

def faceValueOf(card):
    return card.split()[0]
 
def suitOf(card):
    return card.split()[2]

def game(initial):
    bankroll = initial
    counts = 0
    suma = 0
    while 0 < bankroll < 2*initial:
        ronda = random.sample(shuffledDeck(), 6)
        counts += 1
        for cards in ronda:
            if faceValueOf(cards) == 'ace':
                suma += 1
        if suma >= 1:
            bankroll += 1
        else:
            bankroll -= 1
    return counts

initial = float(input('Enter initial: '))
totalcounts = 0
for x in range(1000):
    totalcounts += game(initial)
            
print('Average number of rounds: ', totalcounts/1000)
I'm not getting the right values.

Example of running the CORRECT program:

Enter initial amount: 10
Average number of rounds: 46.582

Enter initial amount: 20
Average number of rounds: 97.506
Reply
#2
hey, any luck with this code? I've been trying to figure out this same question and have only gotten this far -- not entirely sure how to get the numbers right
Reply
#3
I'm not 100% sure about this but my first guess is that it's because you're creating the deck in a loop. You probably need to create the deck beforehand, and re-use it in the loop.
Reply
#4
Stared at this code for way too long before I saw the problem.
suma is set to 0 before entering the while loop. if the first iteration produces "two of hearts", "six of spades", "four of diamonds", "ace of spades", "jack of clubs", then suma will be 1 and the if clause will pass.
Now what happens on the second iteration? suppose the choices are "three of spades", "ten of hearts", "king of spades", "four of clubs", "nine of diamonds". Well, suma is still 1 from the previous iteration, so even though nothing is added, it will still be 1 in the if clause and it will pass again.

The solution? Put suma = 0 within the while loop, not outside of it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Classes, OOP, building deck of 52 playing cards Drone4four 9 3,818 Jan-19-2022, 02:53 PM
Last Post: Drone4four
  Generating random business cards Inkanus 2 2,151 Dec-08-2020, 09:41 PM
Last Post: buran
  python program on cards marc237 3 2,725 Apr-27-2019, 09:26 PM
Last Post: ichabod801
  Need help with simple guessing game! ghost0fkarma 2 2,765 Nov-03-2018, 01:19 AM
Last Post: ghost0fkarma
  Easy equipment system for simple game and problems with it naisyrk 3 3,240 Sep-01-2018, 10:05 AM
Last Post: Gribouillis
  Errors in simple text adventure game? ecloev 5 4,799 Apr-10-2018, 05:55 PM
Last Post: nilamo
  Simple Hangman Game Issue andrew95 2 4,304 Apr-02-2018, 02:24 PM
Last Post: andrew95
  Trouble with simple roulette game chadandersen1 9 7,730 Oct-21-2017, 02:22 AM
Last Post: chadandersen1

Forum Jump:

User Panel Messages

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