Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8603427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:19:12+00:00 2026-06-12T02:19:12+00:00

This is most of my code for my now working python blackjack game (or

  • 0

This is most of my code for my now working python blackjack game (or at least blackjack like game) I have now been told that I need to implement a time limit (user gets asked to input something and only has 3 or so seconds to give a response).

def deck():
        cards = range(1, 12)
        return choice(cards)


def dealer():
    total = deck() + deck()
    if total <= 15:
        totalf = total + deck()
        while totalf <= 21:
            return totalf
    if total > 15:
        return total

def player():
    card1 = deck()
    card2 = deck()
    hand = card1 + card2
    print "Cards dealt: %d and %d" % (card1, card2)
    while hand <= 21:
        choice = raw_input("Would you like to hit or stand?: ")
        print choice
        if choice == "hit":
            hand = hand + deck()
            print "Current Total: %d" % hand        
        elif choice == "stand": 
            return hand 

money = 100
highscore = 0

while money > 0:
    opp = dealer()
    me = player()
    if me > opp:
        highscore = highscore + 10
        money = money + 10
        print "Winner, winner, chicken dinner! You have $%d!" % money
        print "********************************************"
    elif opp > 21:
        highscore = highscore + 10
        money = money + 10
        print "Winner, winner, chicken dinner! You have $%d!" % money
        print "********************************************"
    elif me > 21:
        money = money - 20
        print "Bust! Dealer wins with %d. You have $%d reamaining." % (opp, money)      
        print "********************************************"
    elif opp > me:
        money = money - 20
        print "Dealer wins with %d. You have $%d reamaining." % (opp, money)
        print "********************************************"
    elif me == 21:
        highscore = highscore + 10
        money = money + 10
        print "Blackjack! You have $%d!" % money
        print "********************************************"
    sleep(1)    

print "Thank you for playing! Your highscore was $%d." % highscore

This is the code my professor has provided us with to do this:

   import sys, time
from select import select

import platform
if platform.system() == "Windows":
    import msvcrt

def input_with_timeout_sane(prompt, timeout, default):
    """Read an input from the user or timeout"""
    print prompt,
    sys.stdout.flush()
    rlist, _, _ = select([sys.stdin], [], [], timeout)
    if rlist:
        s = sys.stdin.readline().replace('\n','')
    else:
        s = default
        print s
    return s

def input_with_timeout_windows(prompt, timeout, default): 
    start_time = time.time()
    print prompt,
    sys.stdout.flush()
    input = ''
    while True:
        if msvcrt.kbhit():
            chr = msvcrt.getche()
            if ord(chr) == 13: # enter_key
                break
            elif ord(chr) >= 32: #space_char
                input += chr
        if len(input) == 0 and (time.time() - start_time) > timeout:
            break
    if len(input) > 0:
        return input
    else:
        return default

def input_with_timeout(prompt, timeout, default=''):
    if platform.system() == "Windows":
        return input_with_timeout_windows(prompt, timeout, default)
    else:
        return input_with_timeout_sane(prompt, timeout, default)

I am completely lost how to merge these two pieces of code. I’ve tried for the past couple hours to get it to work but for whatever reason its just not working. Any help would be amazing. (I apologize for the wall of code).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:19:14+00:00Added an answer on June 12, 2026 at 2:19 am

    You just need to call input_with_timeout function where you want the user’s input.

    In your player function:

    def player():
        card1 = deck()
        card2 = deck()
        hand = card1 + card2
        print "Cards dealt: %d and %d" % (card1, card2)
        while hand <= 21:
            choice = input_with_timeout("Would you like to hit or stand?: ", 3, "stand")
            print choice
            if choice == "hit":
                hand = hand + deck()
                print "Current Total: %d" % hand        
            elif choice == "stand": 
                return hand 
    

    will prompt for an input, writing the “Would … or stand” sentence before it. If the user do not answer before the timeout (in this case 3 second) the function will return “stand”.

    And be sure to include your professor’s code in your main file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm relatively new to Ruby on Rails. As of now have this working code
I've been working on this problem most of the morning and I think I
This is a snip-it of python I am working on right now - I
I'm working on a project right now where I have been slowly accumulating a
I've been at this most of the day now, and I cannot get this
I'm a newbie in boost.python and i'm getting this error that I would like
I am trying to use SpinLock, but even this most basic code in a
Title is most of the question, what allows this to be valid code? Is
I will keep this quick. The attached code for the most part works i
I have the .live aspect working now so if links are clicked, my function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.