0

This program is supposed to play the card game War. Okay so I need to be able to prompt the user to keep playing and have them respond by hitting the enter key. I'm not sure how to make this happen. Help?

import cards

# Create a deck of cards
the_deck = cards.Deck()

# Shuffle the deck, then display it in 13 columns
the_deck.shuffle()
print( "===== shuffled deck =====" )
the_deck.display()


def main():
    '''This function deals out half the deck to each
    player. It sorts the rank so as to solve the problem
    with Aces causing an infinite game'''
    player1_list=[]
    player2_list=[]
    for i in range( 26 ):
        p1_temp= the_deck.deal()
        player1_list.append( p1_temp )
        p2_temp= the_deck.deal()
        if (p2_temp.rank()==1):
            player1_list.append(p2_temp)
            player2_list.append(player1_list.pop(0))
        else:
            player2_list.append(p2_temp)


    print()
    # Card dealt to Player #1
    player1_card = player1_list.pop( 0 )
    print( "===== player #1 =====" )
    print( "Card dealt to player #1:", player1_card )
    print( player1_list )
    print()
    #Card dealt to Player #2
    player2_card=player2_list.pop(0)
    print( "===== player #2 =====" )
    print("Card dealt to player #2:",player2_card)
    print( player2_list )

    # Compare the two cards using overloaded operators
    print()
    if player1_card == player2_card:
        print( "Tie:", player1_card, "and", player2_card, "of equal rank" )
    elif player1_card > player2_card:
        print("Player #1 wins:",player1_card,"of higher rank than",player2_card)
    else:
        print("Player #2 wins:",player2_card,"of higher rank than",player1_card)
        print()
main()
def keep_playing():
    '''Determines whether the player wants to continue. If so they press the
    Enter key and the function calls main to start all over.'''
    still_playing=input('Press "Enter" to continue playing')
    Enter=1
    while still_playing==Enter:
        main()
keep_playing()

2 Answers2

0

As @IanAuld mentioned, you have not called your Keep_playing() function anywhere. Instead of calling main(), simply call Keep_playing(). If you'd like to just start the game without the "Press Enter to continue playing," just make the first line of the Keep_playing() function a call to main(), so that it runs and then keeps checking if the user wants to continue.

Also, you have an infinite loop in Keep_playing(). Put the input(...) part inside the while loop after you call main(). That way, it will reprompt every loop and change the still_playing variable.

It should look like this:

def keep_playing():
    '''Determines whether the player wants to continue. If so they press the
    Enter key and the function calls main to start all over.'''
    still_playing=""
    Enter=""
    while still_playing==Enter:
        main()
        still_playing=input('Press "Enter" to continue playing')

keep_playing()

You can read this SO question to figure out how to implement reading the "enter" key as input (it's quite simple, so don't worry)

Additionally, as a good practice in Python (and most other languages), make variables and function names lowercase, as uppercase names are typically reserved for class use. Even if this isn't going to be spread or maintained around, it's good to get into the habit.

Matthew R.
  • 438
  • 4
  • 11
  • Your code has a few errors: one it will always exit (due to *string* 1 not equaling *int* 1); two it will exit on a carriage return as your while loop is checking the value incorrectly (input strips the `\n` so Enter shouldn't be *1*) – LinkBerest Jun 27 '15 at 21:16
  • I was focusing more on the placement of the input/loops within the problem rather than the correct way to parse the input, but you are correct. I have edited my answer to perform (I believe) the desired function. I believe your answer is more sufficient in regards to "proper" code structure, however. – Matthew R. Jun 28 '15 at 06:33
0

Assuming the rest of your main is working (and the imported Deck class): your two main problems are you forgot to call your main and the method you are using to test the variable Enter. Since your taking an input for Enter you just need to test this against an empty string or just test that it is empty - as input strips newlines by default.

def play_hand():
    print ("all the play code goes here")
    #just a simple exit here no return needed
    '''This function deals out half the deck to each
    player. It sorts the rank so as to solve the problem
    with Aces causing an infinite game

    player1_list=[]
    player2_list=[] ... etc ...'''

def keep_playing():
    '''Determines whether the player wants to continue. If so they press the
    Enter key and the function calls main to start all over.'''
    return input('Press "Enter" to continue playing')

def main():
#    the_deck = Card.Deck() # create deck instance (depending on how shuffle method is written this may need to go in while loop)
    still_playing = "" #Set still_playing to empty string to more easily use input
    while not still_playing: #this will keep going as long as still_playing is an empty string
        print("hello main") # cause it fun :) - also as you can put an intro here or the shuffle so:
#        the_deck.shuffle() 
#        print( "===== shuffled deck =====" )
#        the_deck.display()

        play_hand()
        # Do all your other stuff here
        still_playing = keep_playing() #Calls the function and stores returned value (can do this with just input but using return to show an option)

main()

As a secondary note, you need to learn how to isolate the problem within your code. To that end: the following shows a testable instance (and solution) of the enter to continue problem your having. Run this and it will only test (in this case solve) the enter problem, this is what Stack Overflow means by a minimal, complete, verifiable example.

LinkBerest
  • 1,281
  • 3
  • 22
  • 30