Python Forum
randomization code in rock, paper, scissors - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Game Development (https://python-forum.io/forum-11.html)
+--- Thread: randomization code in rock, paper, scissors (/thread-27156.html)



randomization code in rock, paper, scissors - cel - May-27-2020

I'm trying to make a rock paper scissors game with a piece of code that prevents the random function from choosing the same option more than twice but the code isn't working correctly. Any suggestions?

import random

print("Welcome to ROCK PAPER SCISSORS!")
selecter = random.randint(1, 3)

computer_choice = ""


computer_score = 0
player_score = 0
computer_choice_history = []
playing = True
while_counter = 0


while playing:
    while_counter += 1
    if computer_score == 2:
        playing = False
    elif player_score == 2:
        playing = False

    if while_counter > 2:
        print("while counter = " + str(while_counter))
        print(computer_choice_history)
        if computer_choice_history[0] == computer_choice_history[1]:
            selecter = random.randint(1, 3)

    # computer and player choices
    player_choice = input("\nchoose rock, paper or scissors:\n")
    if selecter == 1:
        computer_choice = "rock"
    elif selecter == 2:
        computer_choice = "paper"
    elif selecter == 3:
        computer_choice = "scissors"
        print(computer_choice)
        computer_choice_history.append(selecter)


    # computer wins round
    if computer_choice == "rock" and player_choice == "scissors":
        computer_score += 1
        print("computer score - " + str(computer_score))
        print("your score - " + str(player_score))
    elif computer_choice == "scissors" and player_choice == "paper":
        computer_score += 1
        print("computer score - " + str(computer_score))
        print("your score - " + str(player_score))
    elif computer_choice == "paper" and player_choice == "rock":
        computer_score += 1
        print("computer score - " + str(computer_score))
        print("your score - " + str(player_score))

    # If both chose the same
    if computer_choice == player_choice:
        print("Tie")

    # player wins round
    elif player_choice == "rock" and computer_choice == "scissors":
        player_score += 1
        print("your score - " + str(player_score))
        print("computer score - " + str(computer_score))
    elif player_choice == "scissors" and computer_choice == "paper":
        player_score += 1
        print("your score - " + str(player_score))
        print("computer score - " + str(computer_score))
    elif player_choice == "paper" and computer_choice == "rock":
        player_score += 1
        print("your score - " + str(player_score))
        print("computer score - " + str(computer_score))

if computer_score == 3:
    print("\nComputer wins, YOU LOSE")
elif player_score == 3:
    print("\nCongrats You Win!")

satisfaction = input("\nare you satisfied?\n")
# fix selecter var, line 38#



RE: randomization code in rock, paper, scissors - mcmxl22 - May-28-2020

I think your problem is on line 26.
if computer_choice_history[0] == computer_choice_history[1]:

it should be:
if computer_choice_history[0] == computer_choice_history[0]:

Remember list[0] is the first item and list[1] is the second item in a list.
If you put list[1] when there is only one item in the list you get the out of range error.


RE: randomization code in rock, paper, scissors - perfringo - May-28-2020

I would think about 'algorithm' which determines winner.

One way to approach this: there are only three winning combinations, all other combinations are either loosing or draw.

first_wins = [('Rock', 'Scissors'), ('Paper', 'Rock'), ('Scissors', 'Paper')]
You know the choices of the players and using winning combinations one can create simpler comparison:

if player_choice == computer_choice:
    # draw
elif (player_choice, computer_choice) in first_wins:
    # first, i.e. player wins
else:
    # no draw or first winning, so second i.e. computer wins



RE: randomization code in rock, paper, scissors - GOTO10 - May-28-2020

(May-27-2020, 10:58 PM)cel Wrote: I'm trying to make a rock paper scissors game with a piece of code that prevents the random function from choosing the same option more than twice but the code isn't working correctly. Any suggestions?

To answer your main question, the problem is that your computer_choice_history list is only getting updated if selecter == 3 (due to the indentation level on line 38). You probably mean for lines 37 and 38 to have one less level of indent.

Other problems include your initial assignment of a value to selecter (line 4) being outside of your while loop, so the variable is not getting changed each round and the computer's first two choices will ALWAYS be the same. In line 27, you assign selecter a new random value but don't do anything to ensure that the random choice isn't the same value as before (meaning it's still possible for the computer to play the same choice 3 times in a row).


RE: randomization code in rock, paper, scissors - cel - May-28-2020

Thank you GOTO10 is turned out it was the indentation so now its working.