Home > OS >  Rock, Paper, Scissors Sample Code from book not running properly
Rock, Paper, Scissors Sample Code from book not running properly

Time:03-27

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

CodePudding user response:

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.

CodePudding user response:

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'

  • Related