Python Forum
randomization code in rock, paper, scissors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
randomization code in rock, paper, scissors
#1
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#
Reply
#2
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.
Reply
#3
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
(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).
Reply
#5
Thank you GOTO10 is turned out it was the indentation so now its working.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock/Paper/Scissors Game 1 abscorpy 6 3,800 Dec-01-2020, 10:08 PM
Last Post: metulburr
  rock paper scissors game abscorpy 8 4,542 Feb-14-2019, 07:20 PM
Last Post: abscorpy
  Rock Paper Scissors Game, I need help. extraguac 2 2,538 Feb-14-2019, 04:34 AM
Last Post: extraguac
  [PyGame] Rock,paper,scissors Game(beginner) r0d 10 8,802 Jan-16-2018, 08:01 PM
Last Post: r0d

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020