1

I'm trying to make a simple 2 player game on python 2.7 .

The program will determine the result of a rock, paper, scissors game, given Player 1 and Player 2’s choices. The program will print out the result, score by each player and the total number of game played. My question is:

The code doesn't work when "Rock" is the input.It works when "rock" is the input. Same goes to paper and scissors. How can I make it work?

2 Answers2

1

1.The code doesn't work when "Rock" is the input.It works when "rock" is the input. Same goes to paper and scissors. How can I make it work?

From:

player_1 = str(input(">>Player 1? "))
player_2 = str(input(">>Player 2? "))

Add:

player_1 = str(input(">>Player 1? ")).lower()
player_2 = str(input(">>Player 2? ")).lower()

2.Both player must input their choices before the program can be terminated. That means when player 1's input "-1", the program doesn't terminate immediately. It will proceed to ask player 2 for input before it get terminated. How can I make the program terminate itself immediately when player 1's input is "-1"?

From:

player_1 = str(input(">>Player 1? "))
player_2 = str(input(">>Player 2? "))

Add:

player_1 = str(input(">>Player 1? "))
if (player_1=='-1'):
    print 'End of game'
    break
player_2 = str(input(">>Player 2? "))

3.My code is very long, any suggestions or tips on shortening it without sacrificing any of the function?

use function definitions. Sample:

if (player_1=='-1' or player_2=='-1'):
    print 'End of game'
    break
elif dif in [-1, 2]:
    print ('Player 1 wins.')
    score1 = score1 + 1
    showScore()
elif dif in [1, -2]:
    print('Player 2 wins.')
    score2 = score2 + 1
    showScore()
else:
    print('Tie')
    showScore()

continue

def showScore():
    print '==================='
    print 'Score:'
    print 'Player 1: ' + `score1`
    print 'Player 2: ' + `score2`
    print 'Total game played: ' + `times`
    print ''

Here's a good read

Graham
  • 7,431
  • 18
  • 59
  • 84
Eduard
  • 666
  • 1
  • 8
  • 25
  • I added **.lower()**, but it still doesn't work. It still showing NameError: name 'Rock' is not defined. Thanks for the other two. – Sneaky_Turtle Aug 17 '16 at 06:43
  • Additional question, how can I display something when the input is other than 'rock', 'paper', 'scissors' .I want to display "Please only input 'rock', 'paper', 'scissors' to play the game". – Sneaky_Turtle Aug 17 '16 at 07:28
0

For starters, I converted your program to Python 3. It's better in every way. For one thing, it has a normal definition for input.

In general, if you have N of something, where N is greater than 1, it's better to use an array. If you see repetition, move the data into an array and call a function. When N is 2, you won't necessarily shorten the code (my version is longer than yours) but you'll avoid treating the players differently because they both pass through the same logic.

Put the main logic in a function, too, and reserve the "main" code for dealing with startup & command-line stuff.

When you see a string of elifs, that's also a use data instead indicator. In my victor function, I iterate over tuples of winning combinations. You might consider how to use a dict instead.

import sys, os

def print_results( msg, times, scores ):
    print( (msg) )
    print( '===================' )
    print( 'Score:' )
    print( 'Player 1: %d' % scores[0] )
    print( 'Player 2: %d' % scores[1] )
    print( 'Total game played: %d' % times )
    print( '' )

def victor( inputs ):
    results = ( ('rock', 'scissors'), ('scissors', 'paper'), ('paper', 'rock') );

    for (a, b) in results:
        if a == inputs[0] and b == inputs[1]:
            return 1
        if b == inputs[0] and a == inputs[1]:
            return 2
    return 0

def play(times, scores):
    inputs = ['', '']

    for (i, choice) in enumerate(inputs):
        prompt = '>>Player %d? ' % (i + 1)
        choice = input(prompt).lower()
        if choice == '-1':
            return False
        inputs[i] = choice

    result = victor(inputs)

    if result == 0:
        print_results('Tie', times, scores)
    else:
        scores[result - 1] += 1
        print_results('Player %d wins' % result, times, scores)

    times += 1

    return True


print('''Welcome to play Rock, Paper, Scissors game. Enter -1 to end''')

scores = [0, 0]
times = 0

while play(times, scores):
    pass

if scores[0] == scores[1]:
    player = 'Tie'
else:
    if scores[0] > scores[1]:
        i = 1
    else:
        i = 2
    player = 'Player %d' % i

print( '*******************' )
print( 'Winner: %s' % player )
print( '*******************' )
James K. Lowden
  • 7,574
  • 1
  • 16
  • 31