Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using Python: Define (using variables) the rank and string values. Call the rank variable rank_string. Call the suit variable suit_string. Name the ranks ace, two,

Using Python: Define (using variables) the rank and string values. Call the rank variable rank_string. Call the suit variable suit_string. Name the ranks ace, two, three, etc., in that order. Name the suits clubs, diamonds, hearts, and spades. Create a list called cards. Initialize it to null. Using a set of two for-loops (one inside the other), build the strings for all the cards in the deck. Make the outer loop the suit-loop (for example, for suit in range(4)) and the inner loop the rank-loop. In the inner loop, assign the value of the card string (that is, rank_string[rank] + " of " + suit_string[suit]) to the variable card_string. Still within the inner loop, append (using the append() built-in function) each card_string in turn to the end of the list of cards using the statement cards.append(card_string). Print an output label something like this: The cards are: Create another for-loop to print the cards. Call the counter n. Print all 52 card numbers and values. You will need to print both n and cards[n]. Print them on the same output line. create this define_cards() function: Create the definition statement. In this case, the parameter is n, which represents the value of the card (for example, ace of spades). Insert a statement at the bottom of the define_cards() function in the shuffle_cards.py program that returns the entire list of cards to the calling function. The statement should look like this: return cards[n] Function Definition Section (create_deck) Next, write the create_deck() function to create a deck of cards with the numbers 051 so that you can later change the order of the cards using an existing method from random: Create the definition statement. The parameter for this function is deck. Create a for-loop using i as your counter. Your range value is the number of cards in a deck. The code in the for-loop appends each card number in turn (0 to 51) to the deck. Outside the loop, write a return statement (simply, return) to return control to the calling function in the main processing section. Function Definition Section (shuffle_deck) This function shuffles the deck so that the cards are not in the original order. Random package has a shuffle() function, which you simply need to call. The parameter for this function is deck. Write the shuffle_deck() function in the space below. Hint: Dont forget to put in a return statement to return control to the calling function in the main processing section. Function Definition Section (deal_card) After the program shuffles the cards, it deals the first 10 cards (that is, it turns them over to reveal the card faces). You need a function to do this. Python random routines have a function to do this. Its called pop(), and it pops one item in a list off the top or bottom of the list. Since its not considered fair to deal off the bottom of the deck, well deal off the top. In the space below, write your definition statement (the parameter is deck) and then a return statement that looks like this (the parameter 0 ensures were dealing off the top): return deck.pop(0) Processing Initialization Section Write the statement to initialize the deck to null. (Hint: The variable deck is a list. Be sure you are using the right kind of parentheses for lists.) Processing Section In this section, you need to create the deck, shuffle it, deal 10 cards off the top of the shuffled deck, and print the values (for example, queen of hearts) for the 10 cards as program output. Follow these steps (write the code in the space that follows): 1. Write a statement to call the create_deck() defined function. 2. Write a statement to call the shuffle_deck() defined function. 3. Write an informational message to the user explaining what he is about to see (the 10 dealt cards). 4. Write a for-loop to deal the first 10 cards. You should have two statements within the loop: (1) a statement to call the deal_card() defined function and assign the current value of deck to a variable called card and (2) a statement to print (using the define_card() defined function) the current value of card. Because you go through the loop 10 times, you will print out 10 string values, one by one, for your user to see. you need to print out an informational message to your user. If you were the user instead of the programmer, what would you want to see at the end of the program? (Hint: The user might wonder: Is the program finished? What do I do next?)

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 And Databases

Authors: Jeff Mapua

1st Edition

1978502257, 978-1978502253

More Books

Students also viewed these Databases questions

Question

When does the group seem to be apathetic?

Answered: 1 week ago