Nov-18-2020, 03:42 AM
The question asks to Write a program called test4.py that plays the following card game:
The game starts with certain initial amount of dollars.
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.
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.
Provide a user with an interface to enter the initial bankroll. For each entered number, the program should respond with the average duration of the game for that initial bankroll.
If code is correct it should run the program:
The game starts with certain initial amount of dollars.
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.
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.
Provide a user with an interface to enter the initial bankroll. For each entered number, the program should respond with the average duration of the game for that initial bankroll.
If code is correct it should run the program:
Output:Enter initial amount: 10
Average number of rounds: 46.582
Enter initial amount: 20
Average number of rounds: 97.506
Enter initial amount: 30
Average number of rounds: 148.09
Enter initial amount: 40
Average number of rounds: 194.648
Enter initial amount: 50
Average number of rounds: 245.692
Enter initial amount: 60
Average number of rounds: 290.576
Enter initial amount: 70
Average number of rounds: 335.528
Enter initial amount: 80
Average number of rounds: 391.966
Enter initial amount: 90
Average number of rounds: 433.812
Enter initial amount: 100
Average number of rounds: 487.258
This is my code (but the numbers are incorrect(average):import random faceValues = ['ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king'] suits = ['clubs', 'diamonds', 'hearts', 'spades'] import random 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] import random def oneGame(initial): countFlips = 0 bankroll = initial while 0 < bankroll < 2*initial: d=shuffledDeck() numberOfAces=0 for number in range(6): countFlips+=1 for currentHand in d: if faceValueOf(currentHand)=='ace': bankroll +=1 else: bankroll -= 1 return countFlips def experiment(initials, repetitions): for initial in initials: print('Enter Initial bankroll:', initial) totalFlips = 0 for number in range(repetitions): totalFlips += oneGame(initial) print('Average number of rounds:', totalFlips/repetitions) print() experiment([10,20,30,40,50,60,70,80,90,100],1000)