INPYTHON WRITE A PROGRAM: that deals random cards, printing the \"rank\" of the
ID: 3590689 • Letter: I
Question
INPYTHON WRITE A PROGRAM:
that deals random cards, printing the "rank" of the card and its "suit" ( both randomly generated) until you have dealt 4 aces.
To select a card, you will first choose a random number from 1 - 13 which will represent the "rank" of the card. Numbers from 2 through 10 can be printed just as they are. However, the number 1 should print "Ace", the number 11 should print "Jack", the number 12 should print "Queen" and the number 13 should print "King".
You will also have to select a second random number from 1 to 4 which represents the "suit". This is completely independent of the "rank" described above.
(Notice that each time you draw a card, it is as if it were from a fresh (undealt) deck. Thus, it is possible to draw two or more "Aces of Spades", or two or more "3 of Clubs".)
At the end of the program, you should print out the number of face cards (Jack, Queen or King) that you dealt.
A possible output might look like:
Explanation / Answer
The program has been tested and run in python 2.7
The code goes as follows
#!/usr/bin/python
from random import randint
acecount = 0
rank = ["Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"]
suit = ["Hearts", "Spades", "Clubs", "Diamonds"]
c = 0
f = 0
print "You dealt:"
while acecount != 4:
c+=1
r = randint(0, 12)
s = randint(0, 3)
if r == 0 or r > 9:
f += 1
k = "A " + rank[r]
if r == 0:
acecount+=1
k = "An " + rank[r]
print "Card #",c, ": ", k, " of ", suit[s]
print "There were ",f," Face Cards dealt!"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.