-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
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • 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. – Joffan Mar 27 '22 at 00:40
  • 1
    You seem to have omitted all the indentation from the source code. It will not work without it. – Nick Mar 27 '22 at 00:41
  • You might wanna consider a newer version of the book which is using Python 3 (if there is any). Or another book if there is no Python 3 version. There's really no point learning python 2 these days, except for when you have to work with legacy python 2 projects of course. But as you say you are a beginner, I assume that's not the case. – Mushroomator Mar 27 '22 at 00:45
  • 2
    @Joffan The escaping backslashes might come from the new [ask question wizard](https://meta.stackoverflow.com/questions/416802/punctuation-characters-being-escaped-in-code). – mkrieger1 Mar 27 '22 at 00:50
  • 1
    Does this answer your question? [NameError: name 'raw\_input' is not defined](https://stackoverflow.com/questions/35168508/nameerror-name-raw-input-is-not-defined) – mkrieger1 Mar 27 '22 at 00:51
  • 1
    @mkrieger1 might that also explain the (lack of) indentation issue? – Joffan Mar 27 '22 at 00:53
  • 1
    @Mushroomator I just looked this up on Amazon, and apparently this book came out in 2021? Who is writing a book about Python 2 in 2021? It's not a second edition, either. – Nick ODell Mar 27 '22 at 00:53
  • btw you mistyped while (1 \< 2) it should be while (1 < 2). Which honestly should be while(true) :) – TheHappyBee Mar 27 '22 at 00:55
  • 1
    @TheHappyBee see comments above about escaping backslashes – mkrieger1 Mar 27 '22 at 00:59
  • @Joffan I don't know if that can also cause the lack of indentation. It might be... – mkrieger1 Mar 27 '22 at 00:59
  • @Joffan thank you, changing raw_input to input() worked. Also, the omission of indentation was due to an error when copying in the source code into the text box when creating the question. I'm very new to Stack Overflow (have used it in the past looking at questions but never submitting a question of my own). Thank you for the help everyone. – CupofJoe10 Mar 29 '22 at 00:30

2 Answers2

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.

KingTasaz
  • 161
  • 7
-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'

  • 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](/help/how-to-answer). – Community Mar 27 '22 at 09:37