I wrote my rock paper scissors game for glass, but i used a recursion and not a while loop. I haven't been able to get my while loop to work at all, part of the reason i didn't put it there in the first place. Also, i have to test the input values to. Can someone help me?


import random #Imports the random modual from the library.

def main(): #First function.
x = 'true'
while x=='true':
input = raw_input('Choose 1, 2 or 3')
input = int(input)
if input != 1 or != 2 or != 3:
print 'Invalid input, exiting program'
x = 'false'
else:
PlayerChoice(input)
print 'Lets play Paper Rock Scissors!\n'
print '1 For Rock\n2 For Paper\n3 For Scissors\n4 To Quit'
number=raw_input ('What do you choose? ') #Gets users input.
pc = ComputerChoice()
PlayerChoice(number, pc)

def ComputerChoice(): #Compuers function
ComputerChoice = random.randrange(1, 4) #Computers random range.
return ComputerChoice


def PlayerChoice(number, CC): #Uses the users input & compares
number = int(number) #With the computers.
print "\n"
if CC == 1 and number == 3:
print 'Computer wins: Rock beats Scissors'
elif CC == 1 and number == 2:
print 'Player wins: Paper beats Rock'
elif CC == 2 and number == 3:
print 'Player wins: Scissors beat paper'
elif CC == 3 and number == 1:
print 'Player wins: Rock beats scissors'
elif CC == 2 and number == 1:
print 'Computer wins: Paper beats rock'
elif CC == number:
print '''Draw!''' #Trying it with 3
elif CC == 3 and number == 2:
print 'Computer wins: Scissors beats rock'
elif number == 4:
print 'Goodbye'
else:
print CC
print number
if number != 4:
print '\n'
main()

#Start of program
main()

Thank you!

Recommended Answers

All 11 Replies

Please follow the instructions that you see in the comment text area and put your code inside code blocks. It makes following your code much easier, especially with Python.

Also, please include error messages.

x = 'true'

Should be:

x = True
x = False
if input != 1 or != 2 or != 3:

Should be:

if input != 1 or input != 2 or input != 3:

But something like this is much cleaner:

if input not in (1,2,3):

You also call PlayerChoice at one point with only one parameter.

Sorry about the not wrapping my code.
Where do i put the 'x' for the while function?
I tried tacking it onto the top, but then it says 'x' is not defined.

A lot of the stuff isn't covered in class, and the only examples are nothing more than 5 liners, so writing something like this compared to the examples leaves me looking for help.

Please provide the current state of your code and the problems that you are currently having. Please be specific and show us the exact portion of code that is broken and any error messages that you receive. Do not forget to wrap your code in code tags to preserve formatting.

As this announcement in this forum states, we will not provide homework help to those who do not show effort.

import random  #Imports the random modual from the library.

def main():  #First function.
    x = 'true'
    x = 'false'
while x=='true':
    input = raw_input('Choose 1, 2 or 3')
    input = int(input)
    if input not in (1,2,3):
        print 'Invalid input, exiting program'
        x = 'false'
    else:
        PlayerChoice(input)
    print 'Lets play Paper Rock Scissors!\n'
    print '1 For Rock\n2 For Paper\n3 For Scissors\n4 To Quit'
    number=raw_input ('What do you choose? ') #Gets users input.
    pc = ComputerChoice()
    PlayerChoice(number, pc)

def ComputerChoice(): #Compuers function
    ComputerChoice = random.randrange(1, 4) #Computers random range.
    return ComputerChoice


def PlayerChoice(number, CC): #Uses the users input & compares 
    number = int(number)                             #With the computers.
    print "\n"
    if CC == 1 and number == 3:
        print 'Computer wins: Rock beats Scissors'
    elif CC == 1 and number == 2:
        print 'Player wins: Paper beats Rock'
    elif CC == 2 and number == 3:
        print 'Player wins: Scissors beat paper'
    elif CC == 3 and number == 1:
        print 'Player wins: Rock beats scissors'
    elif CC == 2 and number == 1:
        print 'Computer wins: Paper beats rock'
    elif CC == number:
        print '''Draw!''' #Trying it with 3
    elif CC == 3 and number == 2:
        print 'Computer wins: Scissors beats rock'
    elif number == 4:
        print 'Goodbye'
    else:
        print CC
        print number
    if number != 4:  
        print '\n'
        main()



#Start of program
main()

NameError: name 'x' is not defined

You need to indent everything in main() .

About this:

x = 'true'
x = 'false'

Sorry, I didn't mean you need both. I was just showing you how the keywords True and False work. You should use them instead of the strings 'true' and 'false'.

import random  #Imports the random modual from the library.

def main():  #First function.
    x = 'true'
    x = 'false'
while x=='true':
    input = raw_input('Choose 1, 2 or 3')
    input = int(input)
    if input not in (1,2,3):
        print 'Invalid input, exiting program'
        x = 'false'
    else:
        PlayerChoice(input)
    print 'Lets play Paper Rock Scissors!\n'
    print '1 For Rock\n2 For Paper\n3 For Scissors\n4 To Quit'
    number=raw_input ('What do you choose? ') #Gets users input.
    pc = ComputerChoice()
    PlayerChoice(number, pc)

def ComputerChoice(): #Compuers function
    ComputerChoice = random.randrange(1, 4) #Computers random range.
    return ComputerChoice


def PlayerChoice(number, CC): #Uses the users input & compares 
    number = int(number)                             #With the computers.
    print "\n"
    if CC == 1 and number == 3:
        print 'Computer wins: Rock beats Scissors'
    elif CC == 1 and number == 2:
        print 'Player wins: Paper beats Rock'
    elif CC == 2 and number == 3:
        print 'Player wins: Scissors beat paper'
    elif CC == 3 and number == 1:
        print 'Player wins: Rock beats scissors'
    elif CC == 2 and number == 1:
        print 'Computer wins: Paper beats rock'
    elif CC == number:
        print '''Draw!''' #Trying it with 3
    elif CC == 3 and number == 2:
        print 'Computer wins: Scissors beats rock'
    elif number == 4:
        print 'Goodbye'
    else:
        print CC
        print number
    if number != 4:  
        print '\n'
        main()



#Start of program
main()

NameError: name 'x' is not defined

While you declare x = 'true' and then x = 'false' in your main() function (which makes no sense by the way, as the net is that x will be equal to the string 'false'), main does not get run first.

Python is a scripting language, which means it will execute a script as it sees it; line-by-line. The first line that runs is your import , then the code will "compile" (not really, but that's the easiest way to describe it) the function definition of main. The next line of code that actually runs after the import statement is while x == 'true' . If you meant that statement to be part of your main function you'll need to move it in one indentation level (so that it's actually within the main function).

Do you understand why that line is the second line to be executed? It is not within any code block, so python thinks that you simply want to run it as soon as the program starts up.

You have #Start of program as a comment but that's false, because as you can see the while loop under your main function is on the same level as the call to main() , so instead of calling main() first, the while loop runs.

I hope you understand this, and if not please let me know and I will try to clarify. Another caveat, as it's been mentioned before you should be using the built-in boolean keywords True and False instead of the strings 'true' and 'false' .

Here's a tip: create an empty template and save it somewhere so that when you start a program you can use the same format. I like to make sure there is NO code that is on the base level except for my import statements and my program's "initialization" code. The template I use goes something like this:

""" Welcome to a program template
    This space can be used to provide a description
      of what this program does.
    It helps to provide as much detail as possible """

import os,sys,time # Any other imports here

def main( ):
    # This is the main function
    print 'Hello World!'

def helper_functionA( arg1, arg2, arg3=None):
    # Function definitions can go before or after main
    pass

# Since this if statement is on the base level (no indents)
#  it will be executed immediately after the import statement
if __name__ == '__main__':
    print 'This Program is starting now'
    # Now kick off your main function
    main()

If there's anything in there you need explained don't hesitate to ask...

I can't believe i over looked the while not being indented.
I always had a hard time with the True False, and that's because i never made them with capital letters. I always start with capitol letters, and i'm trying to break that while i write code, but i didn't know they needed to be capitalized.

So could i write portions of code such as a while function, and save it, and use the same one for another script? Because it would be nice to do that with passing arguments too. I get turned around when i'm trying to pass arguments.

You can certainly add things to a file and then import functions selectively within your other programs. If you create classes it will give you even more power behind importing those functions. All depends on how exactly you're looking to do it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.