-2

I am a beginner to Python so the answer to this question might be stupidly simple but I just cant seem to figure it out.

I am making a simple Rock,Paper,Scissors game And imported random and randint to randomise the choice the ‘computer’ makes but after my programme runs once or twice it starts giving ‘IndexError: list index out of range’

I hope you can help me solve this.

A section of my code is as follows:

from random import randint
import time
import sys 

#Creating a list of possibilities
x=['Rock','Paper,''Scissors']

#Computer making a random choice
computer=x[randint(0,2)]

#This sets player to false to help in our While loop
player= False

while player==False:
o11c
  • 15,265
  • 4
  • 50
  • 75
Aziiboii
  • 1
  • 1

3 Answers3

0
x=['Rock','Paper,''Scissors']

This line is the problem -- the comma is in the wrong place. It's inside the string 'Paper,', but it should be outside.

John Gordon
  • 29,573
  • 7
  • 33
  • 58
0

there is a couple things I would recommend changing

in the list of X the comma is in the middle of the string, and you might want to use more descriptive variable names like in "x" rename it to something like "list_of_choices" or "listOfChoices" would be a good idea :). and finally instead of using the "randint" function you should use the choice function it takes in a list as an argument and spits out a random choice.

here's my implementation: import random import time import sys

    #Creating a list of possibilities
    randomChoice = ['Rock','Paper','Scissors']
    computersChoice = random.choice(x)

Hope this helps :D

0

The issue with your code is that you put the comma with the "paper" string instead of putting it after the string. Because there is no comma between paper and scissors, the computer interprets them as one element, meaning the list has a length of 2, not 3. This is the full code. I hope it helps:

from random import choice

# getting input from the user
user_choice = input("Rock, paper or scissors? ").lower()

# printing an error message and quiting the program if the input is invalid
if user_choice != "rock" and user_choice != "paper" and user_choice != "scissors":
    print("Error! Invalid answer!")
    quit()

# generating the computer's choice
options = ["rock", "paper", "scissors"]
computer_choice = choice(options)
print("The computer chose", computer_choice)

# checking all of the possible options and declaring who the winner is
if user_choice == computer_choice:
    print("You both chose", computer_choice, "therefore it's a tie!")
elif user_choice == "rock" and computer_choice == "paper":
    print("Paper beats rock, therefore the computer won!")
elif user_choice == "paper" and computer_choice == "rock":
    print("Paper beats rock, therefore you won!")
elif user_choice == "rock" and computer_choice == "scissors":
    print("Rock beats scissors, therefore you won!")
elif user_choice == "scissors" and computer_choice == "rock":
    print("Rock beats scissors, therefore the computer won!")
elif user_choice == "paper" and computer_choice == "scissors":
    print("Scissors beats paper, therefore the computer won!")
elif user_choice == "scissors" and computer_choice == "paper":
    print("Scissors beats paper, therefore you won!")
Roni
  • 597
  • 5
  • 21