diff --git a/examples/pick_a_card/credit.txt b/examples/pick_a_card/credit.txt new file mode 100644 index 0000000..469477e --- /dev/null +++ b/examples/pick_a_card/credit.txt @@ -0,0 +1 @@ +Problem written by Soumik, popularized by many magicians & their routines \ No newline at end of file diff --git a/examples/pick_a_card/demo.png b/examples/pick_a_card/demo.png new file mode 100644 index 0000000..183739b Binary files /dev/null and b/examples/pick_a_card/demo.png differ diff --git a/examples/pick_a_card/index.html b/examples/pick_a_card/index.html new file mode 100644 index 0000000..5bf951a --- /dev/null +++ b/examples/pick_a_card/index.html @@ -0,0 +1,40 @@ +

+ 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) +

+

Write a program that simulates picking a card randomly from a deck. Here is an example execution of the program:

+

+
+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): y
+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): Y
+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): n
+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): N
+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): N
+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): n
+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): Y
+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): y
+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): *enter pressed*
+
+

+

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.

+ +

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.

+ +

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.

\ No newline at end of file diff --git a/examples/pick_a_card/soln.py b/examples/pick_a_card/soln.py new file mode 100644 index 0000000..eb3d8d1 --- /dev/null +++ b/examples/pick_a_card/soln.py @@ -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() diff --git a/examples/pick_a_card/tags.txt b/examples/pick_a_card/tags.txt new file mode 100644 index 0000000..8401661 --- /dev/null +++ b/examples/pick_a_card/tags.txt @@ -0,0 +1,8 @@ +week 3 +functions +parameters +return +boolean +random + + diff --git a/examples/pick_a_card/title.txt b/examples/pick_a_card/title.txt new file mode 100644 index 0000000..d50a839 --- /dev/null +++ b/examples/pick_a_card/title.txt @@ -0,0 +1 @@ +Pick a Card \ No newline at end of file