Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

is there a way to write this code without using the def keyword and function? Also not using append and join function? import random #variables

is there a way to write this code without using the def keyword and function? Also not using append and join function?

import random #variables player = [] dealer = [] winner = None turn = 'Player' #function to print cards def printCards(cards): cardString = '' for card in cards: cardString = cardString + str(card) + ' ' return cardString #header print('Let\'s Play 21...') #add two random card to both dealer and player dealer.append(random.randint(1,11)) dealer.append(random.randint(1,11)) player.append(random.randint(1,11)) player.append(random.randint(1,11)) #print dealer cards, keeping one card hidden print('Here are the dealer\'s cards:', dealer[0], 'X') #till game ends while(True): #print whose turn it is print('Turn:', turn) #if it's player's turn while(turn == 'Player'): #print player cards and ask choice print('Here are your cards:', printCards(player)) choice = input('What would you like to do? (Hit/Stay) ') #if player card total is 21, player wins if(sum(player) == 21): print('The Player has 21!') winner = 'Player' break #if player card total is more than 21, dealer wins if(sum(player) > 21): print('The Player goes bust!') winner = 'Dealer' break #if player wants to stay, it's dealers turn if(choice.lower() == 'stay'): turn = 'Dealer' break #otherwise player picks new card else: player.append(random.randint(1,11)) #if it's dealer's turn if(turn == 'Dealer'): #print dealer's cards print('Turn:', turn) print('Here are your cards:', printCards(dealer)) #if sum is less than or equal to 16, pick another card if(sum(dealer)  21): print('The Dealer goes bust!') winner = 'Player' #if player's sum is more than dealer's sum, player wins elif(sum(player) > sum(dealer)): print('The Player has', sum(player), 'and the Dealer has', sum(dealer), ".") winner = 'Player' #otherwise dealer wins else: print('The Player has', sum(player), 'and the Dealer has', sum(dealer), ".") winner = 'Dealer' #print the winner print('The', winner, 'wins!') 

The output should be something like this:

image text in transcribed

image text in transcribed

Let's Play 21 Here are the dealer's cards: 7x Turn: Player Here are your cards: 48 What would you like to do? (Hit/Stay) Hit Here are your cards: 488 What would you like to do? (Hit/Stay) stay Turn: Dealer Here are your cards: 75 Your cards total to 16 or less. You must take another card Here are your cards: 759 The Dealer has 21! The Dealer wins! Let's Play 21.. Here are the dealer's cards: 9x Turn: Player Here are your cards: 117 What would you like to do? (Hit/Stay) Stay Turn: Dealer Here are your cards: 95 Your cards total to 16 or less. You must take another card Here are your cards: 959 The Dealer goes bust! The Player wins

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

2nd Edition

0471180742, 978-0471180746

More Books

Students also viewed these Databases questions

Question

What are the main objectives of Inventory ?

Answered: 1 week ago

Question

Explain the various inventory management techniques in detail.

Answered: 1 week ago