| | |
While loop in Rock Paper Scissors
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
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!
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!
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
Also, please include error messages.
Should be:
Python Syntax (Toggle Plain Text)
x = 'true'
Should be:
python Syntax (Toggle Plain Text)
x = True x = False
Last edited by adam1122; Mar 31st, 2009 at 1:09 am.
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
python Syntax (Toggle Plain Text)
if input != 1 or != 2 or != 3:
python Syntax (Toggle Plain Text)
if input != 1 or input != 2 or input != 3:
python Syntax (Toggle Plain Text)
if input not in (1,2,3):
Last edited by adam1122; Mar 31st, 2009 at 1:17 am. Reason: Changed "x" to "input"
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
As this announcement in this forum states, we will not provide homework help to those who do not show effort.
Last edited by jlm699; Mar 31st, 2009 at 2:40 am.
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
Python Syntax (Toggle Plain Text)
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
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
You need to indent everything in
About this:
Sorry, I didn't mean you need both. I was just showing you how the keywords
main() .About this:
Python Syntax (Toggle Plain Text)
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'. •
•
•
•
Python Syntax (Toggle Plain Text)
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
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:
python Syntax (Toggle Plain Text)
""" 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()
Last edited by jlm699; Mar 31st, 2009 at 3:15 am.
![]() |
Similar Threads
- Help with Rock Paper Scissors game (C++)
- rock paper scissor program (C++)
- Whats wrong with this.... (Python)
- Paper Rock Scissors (C++)
- wierd gui/constructor problem (Java)
- nested for loop problem (Java)
- Rock Paper Scissors scoring help (C++)
Other Threads in the Python Forum
- Previous Thread: TypeError: expected a readable buffer object
- Next Thread: fractual curves
| Thread Tools | Search this Thread |
Tag cloud for Python
alarm app beginner cipher cmd code copy cx-freeze data decimals dictionary directory dynamic error examples excel file float font format function generator getvalue gui halp homework http import input ip itunes java keycontrol leftmouse line linux list lists logging loop maintain maze millimeter module mouse mysqldb number numbers output parsing path port prime programming projects push py2exe pygame pyglet pyqt python queue random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode update url urllib urllib2 variable variables ventrilo verify vigenere web webservice wikipedia windows wxpython






