Skip to content

Add a new example pick_a_card #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/pick_a_card/credit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Problem written by Soumik, popularized by many magicians & their routines
Binary file added examples/pick_a_card/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions examples/pick_a_card/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<p>
The first step in almost any card magic trick is to pick a card. It is assumed that the card will be random since, in a typical routine, a standard 52-card deck of French playing cards is thoroughly shuffled. A card can have one of the four suits (Clubs ♣, Diamonds ♦, Hearts ♥, Spades ♠) and one of the thirteen ranks (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King)
</p>
<p> Write a program that simulates picking a card randomly from a deck. Here is an example execution of the program:</p>
<p></p>
<pre class="console" style="width:400px">
Welcome to Pick a Card program! To quit the program, simply press enter

For suits would you like symbols instead of text? (enter y/Y for yes, n/N for no): <span class="blue">y</span>
The randomly picked card was: King of ♥

For suits would you like symbols instead of text? (enter y/Y for yes, n/N for no): <span class="blue">Y</span>
The randomly picked card was: 5 of ♦

For suits would you like symbols instead of text? (enter y/Y for yes, n/N for no): <span class="blue">n</span>
The randomly picked card was: 6 of Hearts

For suits would you like symbols instead of text? (enter y/Y for yes, n/N for no): <span class="blue">N</span>
The randomly picked card was: 5 of Diamonds

For suits would you like symbols instead of text? (enter y/Y for yes, n/N for no): <span class="blue">N</span>
The randomly picked card was: 2 of Diamonds

For suits would you like symbols instead of text? (enter y/Y for yes, n/N for no): <span class="blue">n</span>
The randomly picked card was: Ace of Spades

For suits would you like symbols instead of text? (enter y/Y for yes, n/N for no): <span class="blue">Y</span>
The randomly picked card was: 5 of ♠

For suits would you like symbols instead of text? (enter y/Y for yes, n/N for no): <span class="blue">y</span>
The randomly picked card was: 10 of ♣

For suits would you like symbols instead of text? (enter y/Y for yes, n/N for no): <span class="blue">*enter pressed*</span>
</pre>
<p></p>
<p>If you enter "y" or "Y" (without the double quotes) into the prompt, you will get a visual symbol for the suit of the card. Similarly, if you enter "n" or "N" (without the double quotes) into the prompt, you will get a text for the suit of the card.</p>

<p>For any card, both the suit and the rank of the card should be random. So, it makes sense to have separate functions for each, one to pick a random suit and another to pick a random rank. Both of these functions will use the random.choice() built-in function, which will require importing the random library/module.</p>

<p>Note that the function for picking random suits will need to know if printing symbols instead of printing texts is desired. So depending on the answer, either True or False boolean values will be passed as a parameter to that function. And instead of providing an response, if enter is pressed, the program stops.</p>
63 changes: 63 additions & 0 deletions examples/pick_a_card/soln.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Standard library import
import random


def main():
"""
The purpose of this program is to simulate picking a card randomly
out of a standard 52-card deck of French playing cards.
"""
print("")
print("Welcome to Pick a Card program! To quit the program, simply press enter")
print("")

response = input("For suits, would you like symbols instead of text? (enter y/Y for yes, n/N for no): ")

while response != "": # stop running when the user doesn't enter anything/hits enter

if response == "y" or response == "Y":
rank = pick_a_rank()
suit = pick_a_suit(True) # when we want symbols for suits
print("The randomly picked card was: " + rank + " of " + suit)

elif response == "n" or response == "N":
rank = pick_a_rank()
suit = pick_a_suit(False) # when we want texts for suits
print("The randomly picked card was: " + rank + " of " + suit)

print("") # prints a newline to create spacing
response = input("For suits, would you like symbols instead of text? (enter y/Y for yes, n/N for no): ")

def pick_a_rank():
"""
There are thirteen ranks: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King.
"""
thirteen_ranks_text = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]

random_rank = random.choice(thirteen_ranks_text)

return random_rank


def pick_a_suit(symbols_instead_of_text):
"""
There are four suits: clubs, diamonds, hearts, and spades.
random.choice will pick a random element from the list of suits.
The list of suits will depend on symbols_instead_of_text parameter.
"""
if symbols_instead_of_text:
four_suits_symbols = ["♣", "♦", "♥", "♠"]

random_suit = random.choice(four_suits_symbols)

return random_suit

else:
four_suits_text = ["Clubs", "Diamonds", "Hearts", "Spades"]

random_suit = random.choice(four_suits_text)

return random_suit

if __name__ == "__main__":
main()
8 changes: 8 additions & 0 deletions examples/pick_a_card/tags.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
week 3
functions
parameters
return
boolean
random


1 change: 1 addition & 0 deletions examples/pick_a_card/title.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Pick a Card