Question

In: Computer Science

In this lab assignment, students will demonstrate the abilities to: - Use functions in math module...

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

Solutions

Expert Solution

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

.


Related Solutions

22.8 LAB 5 D FALL 19 : Using math functions This lab problem demonstrates the use...
22.8 LAB 5 D FALL 19 : Using math functions This lab problem demonstrates the use of import module. The Python programming language has many strengths, but one of its best is the availability to use many existing modules for various tasks, and you do not need to be an experienced computer programmer to start using these modules. We have given you some incomplete code; note that the very first line of that code contains an import statement as follows:...
Purpose of Assignment The purpose of the assignment is to develop students' abilities in using data...
Purpose of Assignment The purpose of the assignment is to develop students' abilities in using data sets to apply the concepts of sampling distributions and confidence intervals to make management decisions. Assignment Steps Resources: Microsoft Excel®, The Payment Time Case Study, The Payment Time Case Data Set Review the Payment Time Case Study and Data Set. Develop a 700-word report including the following calculations and using the information to determine whether the new billing system has reduced the mean bill...
Module 06 Assignment – Designing a Care Map Purpose of Assignment Assist students to develop a...
Module 06 Assignment – Designing a Care Map Purpose of Assignment Assist students to develop a care plan that includes safe discharge information for a client with musculoskeletal trauma. Course Competency Explain components of multidimensional nursing care for clients with musculoskeletal disorders. Instructions Mr. Harry Roost is a 78-year old male being discharge after a fracture of his right tibia and fibula. He has a long leg cast that he will need to wear for the next 8 weeks. The...
Module 05 Assignment – Designing a Care Map Purpose of Assignment Assist students with coordination of...
Module 05 Assignment – Designing a Care Map Purpose of Assignment Assist students with coordination of care for clients with musculoskeletal disorders. Enhance understanding of the nursing process in coordinating care for a client. Course Competency Explain components of multidimensional nursing care for clients with musculoskeletal disorders. Select appropriate nursing interventions when providing multidimensional care to clients experiencing alterations in mobility Instructions After reviewing the medical conditions presented in the textbook, including osteoporosis, osteomyelitis, disorders of the feet, plantar fasciitis,...
The number of students using the math lab per day is found in the distribution below....
The number of students using the math lab per day is found in the distribution below. Find the mean, variance, and standard deviation for this probability distribution ? 6 8 10 12 14 ?(?) 0.15 0 0.35 0.1 … Find the (i) Missing probability
In this assignment students will demonstrate their understanding of the distribution of means doing all steps...
In this assignment students will demonstrate their understanding of the distribution of means doing all steps of hypothesis testing. For each problem students will write out all steps of hypothesis testing including populations, hypotheses, cutoff scores, and all relevant calculations. Assignments will be typed and uploaded in a word document to blackboard. 1.A nationwide survey in 1995 revealed that U.S. grade-school children spend an average of µ = 8.4 hours per week doing homework. The distribution is normal with σ...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to create and use menus Demonstrate the ability to create and use an array of structs Demonstrate the ability to generate and use random numbers Program Specifications: Assume that gambling Greg often goes to the Dog Racing Track. Greg loves to bet on the puppies. In each race Greg will place a wager and pick a dog. The dog information will be stored in a...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to...
"Gambling Greg" Assignment Outcomes: Demonstrate the ability to create and use structs Demonstrate the ability to create and use menus Demonstrate the ability to create and use an array of structs Demonstrate the ability to generate and use random numbers Program Specifications: Assume that gambling Greg often goes to the Dog Racing Track. Greg loves to bet on the puppies. In each race Greg will place a wager and pick a dog. The dog information will be stored in a...
The purpose of this lab:  Wireshark Intro Lab is to get students familiar with the use of...
The purpose of this lab:  Wireshark Intro Lab is to get students familiar with the use of their VMs and running wireshark on their VMs. We also examine Ethernet, IPv4, and TCP addressing at the Network Access, Network, and Transport layers of the TCP/IP stack. Reflection: In two paragraphs reflect the experience of using Wireshark capture (in the lab) on the following questions: What was the most valuable feature of the lab? How did you prepare for this lab? What changes...
JAVA Lab Assignment #13:  Looping Lab with both types of loops. This lab demonstrates the use of...
JAVA Lab Assignment #13:  Looping Lab with both types of loops. This lab demonstrates the use of the While Loop and the Do While Loop as error checking mechanisms. You will be using each of these loops to solve the same problem. Please put them both in the same program. When you test the code, you will not see a difference in the way they execute - but there will be a difference in the logic when writing the code. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT