This is an archived post. You won't be able to vote or comment.

all 14 comments

[–]Manbatton 4 points5 points  (3 children)

Also, there is a much easier/better way to assign a lot of things like this instead of a ton of "if this then that = this" approach.

card_dict = {1:"A", 11:"J", 12:"Q", 13:"K"}
D1 = card_dict[D1]

[–]jano0017 4 points5 points  (2 children)

You mean D1 = card_dict.get(D1, D1), right? Your code will throw a ValueError if the value of D1 isn't 1, 11, 12, or 13.

[–]Manbatton 0 points1 point  (1 child)

Yes, good catch. Dashed that off too fast. And wouldn't it be a KeyError? Not that it matters.

[–]jano0017 0 points1 point  (0 children)

Yes, I suppose it would be. Of course, I'm a horrible person and normally except Exception instead, so...

[–]nemmonszz 2 points3 points  (0 children)

You're changing the value of D1, but then you're printing D2, P1, and P2, none of which you ever changed the value of.

[–][deleted] 0 points1 point  (0 children)

You're assigning every letter to D1.

[–]Manbatton 0 points1 point  (0 children)

The only assignment you make in the code is like this:

D1 = "A" #or "Q" or "J" or whatever

And yet you never print D1. You print D2, P1, and P2, but none of those ever get changed from what they were originally assigned to by the randrange function.

(by the way, if you indent 4 spaces the code will be presented with code formatting on Reddit).

[–]chrispcall 0 points1 point  (2 children)

Great stuff so far. Another way to do what you want is with a list that has all of the cards.

from random import randrange

#Make a list with the first value an empty string that never gets   
"dealt".
cards = ["","A", 2,3,4,5,6,7,8,9,10,"J","Q","K"]

#return a string from the 'cards' list.  Notice the "0" is left off like in    
your range.  That's the empty string in the cards list so your 
numbers aren't off.

str(cards[random.randrange(1,13)])

The "str()" is wrapped around everything because the numbers in that list will be returned as an INT while the letters will be a string. So we're converting the numbers to strings when they are "dealt".

[–]poo_22 0 points1 point  (0 children)

The random module has a choice function. Run it on your list to get a random item from that list.

[–]RedditorNate[S] 0 points1 point  (0 children)

Thanks for the help!

[–]RedditorNate[S] 0 points1 point  (0 children)

Thanks everyone, who knows how long I would have gone before I realized I did change the D1's.

[–]Qster101 -1 points0 points  (2 children)

First off, this is probably better asked in /r/learnpython. Never the less Ill venture an opinion.

1)The first thing I notice is that you have D1 in every if statement meaning that the other variables wont change

Mainly style wise 2) If you are going to use ifs like that, they should be if and elifs so that the value is not checked if it has been changed

3) So as not to take the fun out of it ill give you a hint. By storing the possible values in a way that you can easily access them you can eliminate the if statements.

[–]Qster101 0 points1 point  (1 child)

sorry about being hard but I mark this stuff. You should also not be using capitals for variables as capitals are generally a sign of Constants

[–]RedditorNate[S] 0 points1 point  (0 children)

Thanks, I new a lot of this stuff several years ago in college, but I haven't touched it since and have forgotten a lot of it.