Multiple undefined errors


#1

I have multiple errors it would seem but the error messages are things like RPS_play not telling me what’s wrong with it

from random import randint
import time

options = ["R", "P", "S"]
loss = "You lost!"
win = "You won!"

def decide_winner(user_choice, AI_choice):
	print("You chose %s" % user_choice)
	print("AI choosing...")
	time.sleep(2)
	print("The AI chose %s" % AI_choice)
  
	user_choice_index = options.index(user_choice)
	AI_choice_index = options.index(AI_choice)

	if user_choice_inex == AI_choicde_index:
  		print("It's a tie")
	elif user_choice_index == 0 and AI_choice_index == 2:
			print(win)
	elif user_choice_index == 1 and AI_choice_index == 0:
			print(win)
	elif user_choice_index == 2 and AI_choice_index == 1:
			print(win)
	elif user_choice_index > 2:
  		print("Invalid input")
	else:
  		print(loss)
  
def play_RPS():
  print("Welcome to Rock, Paper, Scissors!")
  user_choice = input("Select R for Rock, P for Paper, or S for Scissors: ")
  user_choice.upper
  time.sleep(1)
  AI_choice = options.randint(0, 2)
  
play_RPS()
decide_winner(user_choice, AI_choice)

#2

this program looks like its written in python3, but the lesson uses python2

the problem is here:

user_choice = input("Select R for Rock, P for Paper, or S for Scissors: ")

using input() in python2 will cause the input to validated as code


#3

I tried using raw_input aswell but it isn’t working


#4

user_choice.upper, upper() is a method call, as such, it requires parentheses

here:

AI_choice = options.randint(0, 2)

randint is a function call, not a method call. we need to use the random generated integer as index, how do we access a list (options) by index?

Just saying doesn’t work doesn’t motivate me to help you, if you show me what you improved, what new errors you got, why you think these errors occur, i can help you much more, which improves your learning and understanding


#5
from random import randint
import time

options = ["R", "P", "S"]
loss = "You lost!"
win = "You won!"

def decide_winner(user_choice, AI_choice):
	print("You chose %s" % user_choice)
	print("AI choosing...")
	time.sleep(2)
	print("The AI chose %s" % AI_choice)
  
	user_choice_index = options.index(user_choice)
	AI_choice_index = options.index(AI_choice)

	if user_choice_inex == AI_choicde_index:
  		print("It's a tie")
	elif user_choice_index == 0 and AI_choice_index == 2:
			print(win)
	elif user_choice_index == 1 and AI_choice_index == 0:
			print(win)
	elif user_choice_index == 2 and AI_choice_index == 1:
			print(win)
	elif user_choice_index > 2:
  		print("Invalid input")
	else:
  		print(loss)
  
def play_RPS():
  print("Welcome to Rock, Paper, Scissors!")
  user_choice = input("Select R for Rock, P for Paper, or S for Scissors: ")
  user_choice.upper()
  time.sleep(1)
  AI_choice = options.randint(0, 2)
  
play_RPS()
decide_winner(user_choice, AI_choice)

image


#6

I am not the right person to help you it seems, you clearly use python2, and as i mentioned, in python2, using input() will cause the entered text to be evaluated as code, so you should use raw_input, and nothing of what i mentioned here:

you didn’t do anything of


#7

I tried changing it to raw_input which didn’t work as I said and I have no idea what to do with randint if what I’m doing is wrong


#8

Show me why raw_input isn’t working, just saying it doesn’t work doesn’t give me a picture of why it doesn’t work

you can also not answer me how we access a list by index to get value from the list?


#9


this is what happens when I use raw_input and by index, you mean option[0] right?


#10

that is weird, are you sure you are in the right directory? run pwd to see the directory you are in and ls to see files in the directory (show me the output)

yes, so the square brackets are used to access a list by index, now we need to apply this logic here:

AI_choice = options.randint(0, 2)

to get a random value from the options list


#11

image


#12

the file is called RPS.py, you type PRS.py (you swapped P and R, that is indeed not going to work)

after you typed python you can just press tab (one or two times, not sure, just try) for auto complete


#13

image
still not working


#14

options is the list, so accessing the list by index is done with options[x], where the index x is a random generated number


#15

I’m not following…


#16

you misspelled the filename of the script when you attempted to execute the script


#18

no it’s RPS.py which is the correct filename


#19

yes, but i was still at this image:

where you misspelled it and blamed it on raw_input


#20

with raw_input and spelling it right it still won’t work


#21

because you still have an error at line 35, i showed you what you should do there:


[quote="stetim94, post:14, topic:245284, full:true"]
options is the list, so accessing the list by index is done with options[x], where the index x is a random generated number
[/quote]


agree that we need a value from the list? And that the index should be random?