Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 problems with random number/string
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():
    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"
            displaydealer = dealer
        if playercard == 14:
            displayplayercard = "A"
        elif playercard == 13:
            displayplayercard = "K"
        elif playercard == 12:
            displayplayercard = "Q"
        elif playercard == 11:
            displayplayercard = "J"
            displayplayercard = playercard
            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!
snippsat wrote Mar-01-2019, 03:40 PM:
Added code tag in your post,look at BBCode on how to use.
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.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

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)
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.
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.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Help with a random number generator dpcalder 2 113 Jun-20-2020, 03:50 AM
Last Post: buran
  Make an array of string number in a List polantas 5 222 May-27-2020, 07:18 AM
Last Post: buran
  Generate only one random number for many tries Bhavika 2 168 Mar-29-2020, 12:12 PM
Last Post: Bhavika
  Finding MINIMUM number in a random list is not working Mona 5 407 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  line number of first and second occurance of string in a file mdalireza 1 270 Nov-18-2019, 09:55 AM
Last Post: perfringo
  How can i judge 1st string position is correct number christing 3 366 Oct-30-2019, 03:32 AM
Last Post: newbieAuggie2019
  Beginner at Python. Trying to count certain integer from random string of code kiaspelleditwrong 3 452 Oct-14-2019, 10:40 AM
Last Post: perfringo
  String + Number from List BollerwagenIng 5 517 Aug-25-2019, 07:47 PM
Last Post: perfringo
  same number everytime when using random.randint summeringpainting 5 1,032 Aug-23-2019, 11:01 AM
Last Post: Malt
  TypeError: int() argument must be a string or a number, not 'NoneType' Hadad 2 1,926 Jul-30-2019, 07:22 PM
Last Post: Hadad

Forum Jump:

Users browsing this thread: 1 Guest(s)