-1

I recently bought a book for beginners transitioning into python programming. The book is called "Computer Programming and Cyber Security for Beginners Python by Manuel McFeely." I am not sure how well this book was checked for code errors but one of the sample codes provided does not work. Here is the code below:

"""Rock Paper Scissors - """ import random import os import re os.system('cls' if os.name=='nt' else 'clear') while (1 \< 2): print("\\n") print("Rock, Paper, Scissors - Shoot!") userChoice = raw_input("Choose your weapon \[R\]ock, \[P\]aper, or \[S\]cissors: ") if not re.match("\[SsRrPp\]", userChoice): print("Please choose a letter:") print("\[R\]ock, \[S\]cissors or \[P\]aper.") continue #Echo the user's choice print("You choose: " + userChoice) choices = \['R', 'P', 'S'\] opponenetChoice = random.choice(choices) print("I chose: " + opponenetChoice) if opponenetChoice == str.upper(userChoice): print("Tie!") \#if opponenetChoice == str("R") and str.upper(userChoice) == "P": elif opponenetChoice == 'R' and userChoice.upper() == 'S': print("Scissors beats rock, I win! ") continue elif opponenetChoice == 'S' and userChoice.upper() == 'P': print("Scissors beats paper, I win!") continue elif opponenetChoice == 'P' and userChoice.upper() == 'R': print("Paper beats rock, I win! ") continue else: print("You win!") 

Here is an image of the source code: Source Code

When checking out this sample code, I typed in verbatim the sample code that was provided and tried running the module with no success. When ran, this was the error

Rock, Paper, Scissors - Shoot! Traceback (most recent call last): File "C:\\Users\\parke\\AppData\\Local\\Programs\\Python\\Python310_Training\\Python_McFeely\\Rock Paper Scissors.py", line 11, in \<module\> userChoice = raw_input("Choose your weapon \[R\]ock, \[P\]aper, or \[S\]cissors: ") NameError: name 'raw_input' is not defined 
11
  • 3
    raw_input is a Python 2.x command. The corresponding command in Python 3.x is input. Also, indenting is required for program structure in Python - you cannot ignore it. And in the text shown here, there are a lot of escaping backslashes which simply shouldn't be there. Commented Mar 27, 2022 at 0:40
  • 1
    You seem to have omitted all the indentation from the source code. It will not work without it. Commented Mar 27, 2022 at 0:41
  • 2
    @Joffan The escaping backslashes might come from the new ask question wizard. Commented Mar 27, 2022 at 0:50
  • 1
    Does this answer your question? NameError: name 'raw_input' is not defined Commented Mar 27, 2022 at 0:51
  • 1
    @mkrieger1 might that also explain the (lack of) indentation issue? Commented Mar 27, 2022 at 0:53

2 Answers 2

1

Firstly you spelt "opponent" wrong but thats not the important part. That code was most likely written in an older version of python, so you need to use input instead of raw_input.

If there are any other errors they are most likely related to the same cause.

Sign up to request clarification or add additional context in comments.

Comments

-1

Check this simple code

  1. List item

import random

print("\t-:Welcome to Rcok Paper Scissor:-\n")

def gameWin(comp, you): if comp==you: return None elif comp == 's': if you == 'p': return False elif you == 'r': return True elif comp == 'p': if you == 'r': return False elif you == 's': return True elif comp == 'r'

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.