In: Computer Science
In this lab assignment, students will demonstrate the abilities to: - Use functions in math module - Generate random floating numbers - Select a random element from a sequence of elements - Select a random sample from a sequence of elements (Python Programming)
NO BREAK STATEMENTS AND IF TRUE STATEMENTS PLEASE
Help with the (create) of a program to play Blackjack. In this program, the user plays against the dealer. Please do the following.
(a) Give the user two cards.
You can use the following statements to create a list of cards: cards = ("A","2","3","4","5","6","7","8","9","10","J","Q","K") Use the choice function of the random module twice to draw two cards. J, Q and K each has a value of 10. To make the program easier, A always has a value of 11. Display the two cards drawn and the total value. If the total is 21, display "Blackjack! You have won!" and end the game (You can use the exit() function to end the program).
(b) Use a loop to allow user to draw more cards. Every time a card is drawn, display the card and the updated total. If the total is 21, display "Blackjack! You have won!" and end the game. If the total is higher than 21, display "Bust! You have lost!" and end the game. Otherwise, let the user to decide to draw another card.
(c) Use a loop to draw cards for the dealer. Every time a card is drawn, display the card and the updated total. If the total is 21, display "Blackjack! Dealer has won!" and end the game. If the total is higher than 21, display "Bust! Dealer has lost!" and end the game. The dealer must continue to draw another card until the total is 17 or higher.
(d) If neither the dealer nor the user gets blackjack or bust, compare their totals. If the user's total is higher, display "You have won"; otherwise, display "dealer has won".
The followings are a few examples: Card drawn: 7 Player's Total: 7 Card drawn: 7 Player's Total: 14 Want another card? [y/n] y Card drawn: K Player's Total: 24 Bust! You have lost!
Card drawn: 4 Player's Total: 4 Card drawn: Q Player's Total: 14 Want another card? [y/n] y Card drawn: 3 Player's Total: 17 Want another card? [y/n] y Card drawn: 4 Player's Total: 21 Blackjack! You have won!
Card drawn: 9 Player's Total: 9 Card drawn: 10 Player's Total: 19 Want another card? [y/n] n Card drawn: 3 Dealer's Total: 3 Card drawn: Q Dealer's Total: 13 Card drawn: 10 Dealer's Total: 23 Bust! Dealer has lost!
Card drawn: 6 Player's Total: 6 Card drawn: A Player's Total: 17 Want another card? [y/n] n Card drawn: A Dealer's Total: 11 Card drawn: J Dealer's Total: 21 Blackjack! Dealer has won!
Card drawn: J Player's Total: 10 Card drawn: J Player's Total: 20 Want another card? [y/n] n Card drawn: 3 Dealer's Total: 3 Card drawn: K Dealer's Total: 13 Card drawn: 6 Dealer's Total: 19 Player's total: 20 Dealer's total: 19 You have won!
Card drawn: 10 Player's Total: 10 Card drawn: 10 Player's Total: 20 Want another card? [y/n] n Card drawn: 10 Dealer's Total: 10 Card drawn: K Dealer's Total: 20 Player's total: 20 Dealer's total: 20 Dealer has won!
NO BREAK STATEMENTS PLEASE NO IF TRUE STATEMENTS PLEASE
Code
import random
#tiple of cards
cards = ("A","1","2","3","4","5","6","7","8","9","10","J","Q","K")
#part1
#draw initially two cards
a,b = random.choices(cards,k=2)
v1=0
v2=0
#calculate score from the drawn cards
if a=="J" or a=="Q" or a=="K":
v1=10
if a=="A":
v1=11
if b=="J" or b=="Q" or b=="K":
v2=10
if b=="A":
v2=11
if v1==0:
v1=int(a)
if v2==0:
v2=int(b)
tot=v1
print("Card drawn:",a,"Player's total:",tot)
tot=tot+v2
print("Card drawn:",b,"Player's total:",tot)
#announce winner and exit the game
if(tot==21):
print("Blackjack! Winner!")
exit(0)
#part 2 draw untill players stops
ch = "y"
while(ch=="y" and tot<21):
print("Want another card?[y,n] ",end="")
ch = input()
if(ch=="y"):
a=random.choice(cards)
v1=0
if a=="J" or a=="Q" or a=="K":
v1=10
if a=="A":
v1=11
if v1==0:
v1=int(a)
tot=tot+v1
print("Card drawn:",a,"Player's total:",tot)
#announce win or lose and exit
if(tot==21):
print("Blackjack! You have won the hand!")
exit()
if(tot>21):
print("Burst! You have lost the hand!")
exit()
#part 3
#draw untill dealer scores >=17
d=0
while(d<17):
a=random.choice(cards)
v1=0
if a=="J" or a=="Q" or a=="K":
v1=10
if a=="A":
v1=11
if v1==0:
v1=int(a)
d=d+v1
print("Card drawn:",a,"Dealer's total:",d)
if(d==21):
print("Blackjack! Dealer have won the hand!")
exit()
if(d>21):
print("Burst! Dealer have lost the hand!")
exit()
#part4
#announce the winner
if(tot>d):
print("You have won")
elif(d>tot):
print("dealer has won")
#if both scores are equal
else:
print("no-one won")
Terminal Work
.