Python Forum
problems with random number/string - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: problems with random number/string (/thread-16476.html)



problems with random number/string - PrasadPython - Mar-01-2019

I am VERY new to Python (2 weeks in!) and was trying a random test of things I've learned so far (basics with if statements, print commands, etc.) I'm sure there's a better way to do what I'm trying to do, but for now I don't know classes, dictionaries or other techniques that might be helpful. I'm basically trying to create a deck of cards using a random number generator, but when the number is an 11 through a 14 I would like it to display a text string ("J", "Q" for example). What I'm finding is that it doesn't seem to be working for any numbers above 10. I set up the "while" loop, but that was just for testing purposes so I could run multiple iterations at once and see if it was ever resulting in face cards. Can somebody help me with what I'm doing wrong? Again...I'm just a novice, so I very much appreciate the help. Code below!
#Mechanics for random card draw
import random
def deal():
    n=5
    while n>0:
        dealer = random.randint(2,14)
        playercard = random.randint(2,14)
        if dealer == 14:
            displaydealer = "A"
        elif dealer == 13:
            displaydealer = "K"
        elif dealer == 12:
            displaydealer = "Q"
        elif dealer == 11:
            displaydealer = "J"
        else:
            displaydealer = dealer
            print(displaydealer)
        if playercard == 14:
            displayplayercard = "A"
        elif playercard == 13:
            displayplayercard = "K"
        elif playercard == 12:
            displayplayercard = "Q"
        elif playercard == 11:
            displayplayercard = "J"
        else:
            displayplayercard = playercard
            print(displayplayercard)
            n = n-1
NOTE: I don't know how to make indentations show up in my thread, but it is indented properly in my text editor. I could use help there too!


RE: problems with random number/string - ichabod801 - Mar-01-2019

Your print calls are indented under your else statements. That makes them part of the code run by the else statement, and that code is not run by the if or elif statements. You need to unindent the print calls, so that they print for any value of dealercard/playercard.

You can do this much easier with string indexing:

import random

ranks = 'X123456789TJQKA'
suits = 'CDHS'

rank = random.randint(1, 14)
suit = random.randrange(4)

print(ranks[rank], suits[suit])
Play around with the above code and try to figure out how it is working.


RE: problems with random number/string - PrasadPython - Mar-01-2019

Thank you, that's helpful. I can see what your code is doing, though I assume the "X" in the first part of your string is some syntax thing with python where randint doesn't return the lowest number in your range? Could you also make the range from 0,13 and eliminate the x?

I like how you did this, because it allows me to retain the numerical value of the card (A = 14 for example), but if I wanted to write out the full word "Ace" I don't think this would work, correct? Instead, I'm trying to do this now with a slight modification.

ranks = [1,2,3,4,5,6,7,8,9,10,'Jack','Queen','King','Ace']
rank = random.randint(1,14)
print(ranks[rank])
print(rank)
I put the final print(rank) command just to see if the integer is lining up with the numerical value of the card (it isn't...), but I think I just have to play with it because I'm sure this is just the way that function works.


RE: problems with random number/string - ichabod801 - Mar-01-2019

Python is called zero-indexed. So the first item in a list or string has the index of zero, the second item has an index of one, and so on. So my early code was a little off. If you put a zero at the beginning of your ranks list, and use random.randint(2, 14), it should work pretty well.