My program does not print out what the computer chooses and the points - I made a "Rock, Paper, Scissors" program

时间:2019-04-08 12:54:03

标签: python

I'm making a "Rock, Paper, Scissors" game on Python. However, there is one big problem - it doesn't print what the computer chose and the points, so I don't know whether I'm winning at that point or not.

I tried printing "player_choice", "python_choice", "p_player_choice" and "p_python_choice". It printed out the correct variable, but not the points or what the computer chose.

The code is below:

import random
player_points = 0
player_points = 0
python_points = 0
p_player_choice = 0
p_python_choice = 0

loopnum = 3
for number in range(loopnum):
    print("1 - Rock")
    print("2 - Paper")
    print("3 - Scissors")
    print()

    print("What do you choose?\n")
    player_choice = input()
    python_choice = random.randint(1,3)

    if (player_choice == 1):
        p_player_choice = "rock"
    if (player_choice == 2):
        p_player_choice = "paper"
    if (player_choice == 3):
        p_player_choice = "scissors"

    if (python_choice == 1):
        p_python_choice = "rock"
    if (python_choice == 2):
        p_python_choice = "paper"
    if (python_choice == 3):
        p_python_choice = "scissors"



    if (player_choice == python_choice):
        print("The computer chose ",p_python_choice,"!",sep="")
        player_points =+ 1
        python_points =+ 1
        print()
        print("Player -",player_points)
        print("Computer -",python_points)

    if (player_choice == 1) and (python_choice == 2):
        print("The computer chose ",p_python_choice,",so it gets a 
        point!",sep="")
        python_points =+ 1
        print()
        print("Player -",player_points)
        print("Computer -",python_points)

    if (player_choice == 1) and (python_choice == 3):
        print("The computer chose ",p_python_choice,",so you get a 
        point!",sep="")
        player_points =+ 1
        print()
        print("Player -",player_points)
        print("Computer -",python_points)

    if (player_choice == 2) and (python_choice == 1):
        print("The computer chose ",p_python_choice,",so you get a 
        point!",sep="")
        player_points =+ 1
        print()
        print("Player -",player_points)
        print("Computer -",python_points)

    if (player_choice == 2) and (python_choice == 3):
        print("The computer chose ",p_python_choice,",so it gets a 
        point!",sep="")
        python_points =+ 1
        print()
        print("Player -",player_points)
        print("Computer -",python_points)

    if (player_choice == 3) and (python_choice == 1):
        print("The computer chose ",p_python_choice,",so it gets a 
        point!",sep="")
        python_points =+ 1
        print()
        print("Player -",player_points)
        print("Computer -",python_points)

    if (player_choice == 3) and (python_choice == 2):
        print("The computer chose ",p_python_choice,",so you get a 
        point!",sep="")
        player_points =+ 1
        print()
        print("Player -",player_points)
        print("Computer -",python_points)

print()

if ((player_points == 3) and (python_points<3)):
    print("You win!")


if ((python_points == 3) and (player_points<3)):
    print("You lose!")


if ((python_points == 3) and (player_points == 3)):
    print("You draw!")

else:
    print("You both lose!")

Expected Result:

1 - Rock
2 - Paper
3 - Scissors

What do you choose?

>>>2

The computer chose rock, so you get a point!

Player - 2
Computer - 1

Actual Result:

1 - Rock
2 - Paper
3 - Scissors

What do you choose?

>>>2

1 - Rock
2 - Paper
3 - Scissors

What do you choose?

1 个答案:

答案 0 :(得分:0)

In this line

player_choice = input()

Your script is reading the characters you have entered. The script does not know that these characters are supposed to be a number between 1 and 3, but the characters are interpreted as a string, i.e. as you would write "2" in your script. Thus none of you if statements matches.

You would have to convert player_choice into a number e.g. by writing

player_choice = int(input())

This will convert what is read at the prompt into an integer.