1

So, I'm trying to make a blackjack game. A very simple one, if I may add.

My code currently looks like this:

def cmd_blackjack(message):
    playblackjack(message, player=True)

def dealcard():
    card = random.randrange(1, 11)
    return card

def dealhand(message, player=False, dealer=False):
    card1 = dealcard()
    card2 = dealcard()
    if player:
        client.send_message(message.channel,
                               'BLACKJACK: Your cards: %s and %s \n Type !hitme for more cards or !stay to stay' % (card1, card2))
    if dealer:
        client.send_message(message.channel,
                               "BLACKJACK: Dealer's cards: %s %s" % (card1, card2))
    return card, card2

def playblackjack(message, player=False, dealer=False):
    dealhand(player)

And this is pretty much what I'm trying to archieve:

def playblackjack(message, player=False, dealer=False):
    dealhand(player)
    // This is when the player has to input !hitme to get more cards
    if not playerhastypedhitme in 300 secs:
        return

    dealhand(player=False, dealer=True)
    // code continues

So basically, I need to figure out a (non-retarded, I know I can do it with lists, for example) way to make the function wait for user input. Like make an another function send an 'OK, continue' message to this function.

I know this has been asked before probably, It's just very hard describing in search terms what I want to accomplish

user3553653
  • 127
  • 1
  • 8
  • http://stackoverflow.com/questions/1335507/keyboard-input-with-timeout-in-python – Roger Heathcote Nov 10 '16 at 20:50
  • "_I know I can do it with lists, for example_". Can you show us how you did it with lists? It might make a good starting point. – Kevin Nov 10 '16 at 20:52
  • I didn't try using lists on this particular thing, but I used them in my previous coding expeditions, back when I was even more amateur. Basically, after one function finished, it would append a number to list, and then another function checks if that list has a number.. Now when I think about it, I don't know how I would use it here, heh. – user3553653 Nov 10 '16 at 21:16

2 Answers2

2

You don't need to sleep. If I understood correctly, you just want to wait for user input.

Python's input() function does exactly that for you.

joaquinlpereyra
  • 956
  • 7
  • 17
  • So how do you make `input` return if the user doesn't type anything in 300 seconds? – Kevin Nov 10 '16 at 20:52
  • Only way I can think of it is with threading, but I'm sure you don't want to go there. I found this: http://stackoverflow.com/questions/2933399/how-to-set-time-limit-on-input, and the platform-independent way indeed uses threads. It's up to you to decide if it's worth it. – joaquinlpereyra Nov 10 '16 at 21:07
  • Thanks, but I didn't figure out how input would work here. The sleeping isn't important, I would just like to see this work with this first. So It's something like, if input('!hitme') ? – user3553653 Nov 10 '16 at 21:18
0

There was a built-in function in discord.py that saved me. The end result can be found here: https://github.com/Thomaxius/lemon_bot_discord

user3553653
  • 127
  • 1
  • 8