Python Random Playing Card Generator Game -
i'm attempting create program in python allow random card created using random function, suit , card number.
code far shown below...
import random num1 = random.randint(1,13) num2 = random.randint(1,4) cardnum1 = "" cardnum2 = "" input ("press enter key continue \n") if true: if num1 == 11: cardnum1 = "queen" elif num1 == 12: cardnum1 = "jack" elif num1 == 13: cardnum1 = "king" elif num1 < 10: cardnum1 = num1 elif num2 == 1: cardnum2 = "spades" elif num2 == 2: cardnum2 = "hearts" elif num2 == 3: cardnum2 = "diamonds" elif num2 == 4: cardnum2 = "clubs" print (cardnum1, cardnum2)
if card number (num1) 11, 12 or 13, card jack, queen , king respectively. if variable num22 equal 1, 2, 3 or 4, card spades, hearts, diamonds , clubs respectively too.
the issue have here rather having both card number , suit printed together, idle prints card number , chooses not print variable cardnum2. example, if num1 8 , num2 diamonds, expect idle print "9 diamonds" instead prints "9". can choose method solving , writing program, can please clear code , me code?
thanks, jack.
update: can ask, how can loop program repeatedly creates new cards when user presses enter key?
the problem in big if: elif:
block:
elif num1 < 10: cardnum1 = num1 elif num2 == 1: cardnum2 = "spades"
you want deal num2
separately num1
, should be:
elif num1 < 10: cardnum1 = num1 if num2 == 1: cardnum2 = "spades"
or, much simpler, use pair of dictionaries, idiomatic replacement whole bunch of elif
s:
faces = {11: "queen", 12: "jack", 13: "king"} # it's j q k, though cardnum1 = faces.get(num1, num1) suits = {1: "spades", 2: "hearts", 3: "diamonds", 4: "clubs"} cardnum2 = suits[num2]
Comments
Post a Comment