While loop in Rock Paper Scissors

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2009
Posts: 6
Reputation: tschafer204 is an unknown quantity at this point 
Solved Threads: 0
tschafer204 tschafer204 is offline Offline
Newbie Poster

While loop in Rock Paper Scissors

 
0
  #1
Mar 31st, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: While loop in Rock Paper Scissors

 
0
  #2
Mar 31st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: While loop in Rock Paper Scissors

 
0
  #3
Mar 31st, 2009
Also, please include error messages.

  1. x = 'true'

Should be:

  1. x = True
  2. x = False
Last edited by adam1122; Mar 31st, 2009 at 1:09 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: While loop in Rock Paper Scissors

 
0
  #4
Mar 31st, 2009
  1. if input != 1 or != 2 or != 3:
Should be:
  1. if input != 1 or input != 2 or input != 3:
But something like this is much cleaner:
  1. if input not in (1,2,3):
Last edited by adam1122; Mar 31st, 2009 at 1:17 am. Reason: Changed "x" to "input"
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: While loop in Rock Paper Scissors

 
0
  #5
Mar 31st, 2009
You also call PlayerChoice at one point with only one parameter.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: tschafer204 is an unknown quantity at this point 
Solved Threads: 0
tschafer204 tschafer204 is offline Offline
Newbie Poster

Re: While loop in Rock Paper Scissors

 
0
  #6
Mar 31st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,063
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: While loop in Rock Paper Scissors

 
0
  #7
Mar 31st, 2009
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.
Last edited by jlm699; Mar 31st, 2009 at 2:40 am.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 6
Reputation: tschafer204 is an unknown quantity at this point 
Solved Threads: 0
tschafer204 tschafer204 is offline Offline
Newbie Poster

Re: While loop in Rock Paper Scissors

 
0
  #8
Mar 31st, 2009
  1. import random #Imports the random modual from the library.
  2.  
  3. def main(): #First function.
  4. x = 'true'
  5. x = 'false'
  6. while x=='true':
  7. input = raw_input('Choose 1, 2 or 3')
  8. input = int(input)
  9. if input not in (1,2,3):
  10. print 'Invalid input, exiting program'
  11. x = 'false'
  12. else:
  13. PlayerChoice(input)
  14. print 'Lets play Paper Rock Scissors!\n'
  15. print '1 For Rock\n2 For Paper\n3 For Scissors\n4 To Quit'
  16. number=raw_input ('What do you choose? ') #Gets users input.
  17. pc = ComputerChoice()
  18. PlayerChoice(number, pc)
  19.  
  20. def ComputerChoice(): #Compuers function
  21. ComputerChoice = random.randrange(1, 4) #Computers random range.
  22. return ComputerChoice
  23.  
  24.  
  25. def PlayerChoice(number, CC): #Uses the users input & compares
  26. number = int(number) #With the computers.
  27. print "\n"
  28. if CC == 1 and number == 3:
  29. print 'Computer wins: Rock beats Scissors'
  30. elif CC == 1 and number == 2:
  31. print 'Player wins: Paper beats Rock'
  32. elif CC == 2 and number == 3:
  33. print 'Player wins: Scissors beat paper'
  34. elif CC == 3 and number == 1:
  35. print 'Player wins: Rock beats scissors'
  36. elif CC == 2 and number == 1:
  37. print 'Computer wins: Paper beats rock'
  38. elif CC == number:
  39. print '''Draw!''' #Trying it with 3
  40. elif CC == 3 and number == 2:
  41. print 'Computer wins: Scissors beats rock'
  42. elif number == 4:
  43. print 'Goodbye'
  44. else:
  45. print CC
  46. print number
  47. if number != 4:
  48. print '\n'
  49. main()
  50.  
  51.  
  52.  
  53. #Start of program
  54. main()

NameError: name 'x' is not defined
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: While loop in Rock Paper Scissors

 
0
  #9
Mar 31st, 2009
You need to indent everything in main() .

About this:
  1. x = 'true'
  2. 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'.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,063
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: While loop in Rock Paper Scissors

 
0
  #10
Mar 31st, 2009
Originally Posted by tschafer204 View Post
  1. import random #Imports the random modual from the library.
  2.  
  3. def main(): #First function.
  4. x = 'true'
  5. x = 'false'
  6. while x=='true':
  7. input = raw_input('Choose 1, 2 or 3')
  8. input = int(input)
  9. if input not in (1,2,3):
  10. print 'Invalid input, exiting program'
  11. x = 'false'
  12. else:
  13. PlayerChoice(input)
  14. print 'Lets play Paper Rock Scissors!\n'
  15. print '1 For Rock\n2 For Paper\n3 For Scissors\n4 To Quit'
  16. number=raw_input ('What do you choose? ') #Gets users input.
  17. pc = ComputerChoice()
  18. PlayerChoice(number, pc)
  19.  
  20. def ComputerChoice(): #Compuers function
  21. ComputerChoice = random.randrange(1, 4) #Computers random range.
  22. return ComputerChoice
  23.  
  24.  
  25. def PlayerChoice(number, CC): #Uses the users input & compares
  26. number = int(number) #With the computers.
  27. print "\n"
  28. if CC == 1 and number == 3:
  29. print 'Computer wins: Rock beats Scissors'
  30. elif CC == 1 and number == 2:
  31. print 'Player wins: Paper beats Rock'
  32. elif CC == 2 and number == 3:
  33. print 'Player wins: Scissors beat paper'
  34. elif CC == 3 and number == 1:
  35. print 'Player wins: Rock beats scissors'
  36. elif CC == 2 and number == 1:
  37. print 'Computer wins: Paper beats rock'
  38. elif CC == number:
  39. print '''Draw!''' #Trying it with 3
  40. elif CC == 3 and number == 2:
  41. print 'Computer wins: Scissors beats rock'
  42. elif number == 4:
  43. print 'Goodbye'
  44. else:
  45. print CC
  46. print number
  47. if number != 4:
  48. print '\n'
  49. main()
  50.  
  51.  
  52.  
  53. #Start of program
  54. 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:
  1. """ Welcome to a program template
  2. This space can be used to provide a description
  3. of what this program does.
  4. It helps to provide as much detail as possible """
  5.  
  6. import os,sys,time # Any other imports here
  7.  
  8. def main( ):
  9. # This is the main function
  10. print 'Hello World!'
  11.  
  12. def helper_functionA( arg1, arg2, arg3=None):
  13. # Function definitions can go before or after main
  14. pass
  15.  
  16. # Since this if statement is on the base level (no indents)
  17. # it will be executed immediately after the import statement
  18. if __name__ == '__main__':
  19. print 'This Program is starting now'
  20. # Now kick off your main function
  21. main()
If there's anything in there you need explained don't hesitate to ask...
Last edited by jlm699; Mar 31st, 2009 at 3:15 am.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC