2

Problems: Program does not seem to accept the integers entered. Won't add to win/loss/draw count and does not display computer choice in debug mode

Basics Design of the Program: Write a program that lets the user play the game of Rock, Paper, Scissors against the computer. The program should work as follows. A menu is displayed:

Score: 0 wins, 0 draws, 0 losses (D)ebug to show computer's choice (N)ew game (Q)uit

If the user enters "Q" or "q" the program would end. "N" or "n" for a new game, "D" or "d" for debug mode, anything else would cause an error message to be displayed.

  1. When a game begins, a random number in the range of 1 through 3 is generated. If the number is 1, then the computer has chosen rock. If the number is 2, then the computer has chosen paper. If the number is 3, then the computer has chosen scissors. (Don't display the computer's choice yet unless we are in "D"ebug mode.)
  2. The user enters his or her choice of “1-rock”, “2-paper”, or “3-scissors” at the keyboard.
  3. The computer's choice is displayed.
  4. A winner is selected according to the following rules: • If one player chooses rock and the other player chooses scissors, then rock wins. (The rock smashes the scissors.) • If one player chooses scissors and the other player chooses paper, then scissors wins.(Scissors cuts paper.) • If one player chooses paper and the other player chooses rock, then paper wins. (Paper wraps rock.) • If both players make the same choice, the game is a draw.
  5. Your program would keep a running total of the number of wins, loses and draws.
  6. Re-display the menu and repeat the game loop.

My Program:

import random

def main():

    continuing = "y"

    win = 0
    lose = 0
    draw = 0

    while continuing == "y":
        print("Score:", win,"wins,", draw, "draws,", lose,"losses")
        print("(D)ebug to show computer's choice")
        print("(N)ew game")
        print("(Q)uit")

        choice = input(" ")

        if choice == "n" or choice == "N":
            win, draw, lose = playgame(win, draw, lose)

        elif choice == "d" or choice == "D":
            win, draw, lose = playgame2(win, draw, lose)

        elif choice == "q" or choice == "Q":
            break


def playgame(win, draw, lose):

    computer = random.randint(1,3)
    player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")

    if computer == 1 and player == 2:
        Score = "You won"
        win += 1

    elif computer == 1 and player == 3:
        Score = "You lost"
        lose += 1

    elif computer == 2 and player == 1:
        Score = "You lost"
        lose += 1

    elif computer == 2 and player == 3:
        Score = "You won"
        win += 1

    elif computer == 3 and player == 1:
        Score = "You won"
        win += 1

    elif computer == 3 and player == 2:
        Score = "You lost"
        lose += 1

    elif computer == player:
        Score = "Draw"
        draw += 1

    return (win, draw, lose)

def playgame2(win, draw, lose):

    computer = random.randint(1, 3)
    player = input("Enter 1 for Rock, 2 for Paper, or 3 for Scissors: ")

    if computer == 1 and player == 2:
        Score = "You won"
        print("Computer chose rock")
        win += 1

    elif computer == 1 and player == 3:
        Score = "You lost"
        print("Computer chose rock")
        lose += 1

    elif computer == 2 and player == 1:
        Score = "You lost"
        print("Computer chose paper")
        lose += 1

    elif computer == 2 and player == 3:
        Score = "You won"
        print("Computer chose paper")
        win += 1

    elif computer == 3 and player == 1:
        Score = "You won"
        print("Computer chose scissors")
        win += 1

    elif computer == 3 and player == 2:
        Score = "You lost"
        print("Computer chose scissors")
        lose += 1

    elif computer == player:
        Score = "Draw"
        print("Computer chose the same as you")
        draw += 1

    return (win, draw, lose)


main()           
Chachmu
  • 7,725
  • 6
  • 30
  • 35
Ryan A
  • 23
  • 3

2 Answers2

4

I'm no Pythonista, but at a guess, input returns strings, and you'll need to convert to integer before comparing to the computer's int.

I also think you are missing a trick in DRYing up your code - you should be able to have a single playgame method, which takes an additional boolean parameter debugmode, which instead of calling print directly, calls an indirection, e.g.:

def debugPrint(debugString, debugMode)
     if debugMode
         print(debugString)

Hope this makes sense?

Community
  • 1
  • 1
StuartLC
  • 104,537
  • 17
  • 209
  • 285
0

This would work in Python 2.x, but, not in Python 3.x In Python 3.x, input() returns strings. Thus, the player's input would be of the form "1" or "2" or "3". Since 1 and "1" are different, the program will not execute any of the lines in the if and elif blocks in playgame() and playgame2(). Here is a Python 3.x example:

>>> a = input("Input = ")
Input = 1
>>> print a
SyntaxError: Missing parentheses in call to 'print'
>>> print(a)
1
>>> a
'1'
>>> type(a)
<class 'str'>

Thus, you should use i = int(input("Input = ")) wherever you want an integer input.

However, in Python 2.x, input() will take 1 as 1 itself and not as "1". But, when you want to type a string as an inpu, you will have to give the quotes also. Here is an exxample:

>>> a1 = input("Input = ")
Input = 1
>>> a1
1
>>> type(a1)
<type 'int'>
>>> #I want to input a string now:
>>> a2 = input("Input = ")
Input = string

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    a2 = input("Input = ")
  File "<string>", line 1, in <module>
NameError: name 'string' is not defined
>>> a2 = input("Input = ")
Input = "string"
>>> a2
'string'
>>> type(a2)
<type 'str'>
>>> a3 = raw_input("Input = ")
Input = hello
>>> a3
'hello'
>>> type(a3)
<type 'str'>
>>> 

In Python 2.x, the raw_input() function takes the input as a string.