-2

Here is the code...

import random
Rock = '1'
Paper = '2'
Scissors = '3'
print('Welcome to Rock, Paper Scissors! The game of all kids to decide on something. \nIn this game you will have to beat the computer once. \n(Psst if it\'s a draw the start the program again! ;D)\nSo how to play. Well, it\'s simple. Pick 1 for Rock, 2 for Paper and 3 for Scissors. \nSo Rock beats Scissors. Scissors cuts Paper and Paper covers Rock. Got it Lets play')
player=int(input('Please enter number 1 = Rock, 2 = Paper, 3 = Scissors: '))
if player<1 or player>3 except player==9:
   player=int(input('Invalid number. Please enter number 1 = Rock, 2 = Paper, 3 = Scissors: '))
   if player<1 or player>3:
       print('Well, now you can\'t play this game because you are mucking around. Next time DON\'T!')
elif player==9:
    exit()
else:
   computer=random.randint(1, 3)
   print(player,computer)
   print('Remember Rock = 1, Paper = 2 and Scissors = 3')
   if player==1 and computer==1 or player==2 and computer==2 or player==3 and computer==3:
       print('It\'s a draw. =l Restart the game if you want to.')
   if player==1 and computer==2 or player==2 and computer==3 or player==3 and computer==1:
       print('Computer wins! You lose. Sorry. =(')
   if player==1 and computer==3 or player==2 and computer==1 or player==3 and computer==2:
       print('You have won. Well done. =D')

I need to loop it so that when the user inputs a wrong number it is then asked again until the user has answered properly. Also I need to loop the program to play until the user presses 9 to exit.

user1883616
  • 15
  • 1
  • 2
  • 5
  • if you know what a loop is, here i present you python's `while/for`; look for any of them, try to write the loop, and then ask for help on what have gone wrong – Rubens Jan 01 '13 at 20:26
  • Have you taken a look at http://stackoverflow.com/questions/743164/do-while-loop-in-python ? – Carlos Jan 01 '13 at 20:29

4 Answers4

3

Using a loop you can control when the game ends.

game_over = False
while not game_over:
    do_stuff()
    if some_condition:
        game_over = True

At the end of the loop, it checks if the game is over. If it is, the loop exits and any code after the loop is run. Then the program exists.

Brigand
  • 84,529
  • 20
  • 165
  • 173
1

The smallest change you can make, to have this keep looping is to just add a while True statement, and nest the rest of your game under it. I would also fix up your test for the exit case like so...

...
print('Welcome to Rock, Paper Scissors! ...')
while True:  # loop forever
  player=int(input('Please enter number 1 = Rock, 2 = Paper, 3 = Scissors: '))
  if player==9:  # but exit if 9
      exit()
  elif player<1 or player>3:
     ...
  else:
     ...
jdi
  • 90,542
  • 19
  • 167
  • 203
1

It should be as simple as a loop like this.

while True:
  while player not in (1,2,3,9):
    keep_asking_for_input()
  if player == 9:
    break
  pass
hcarver
  • 7,126
  • 4
  • 41
  • 67
0

Rewrite your code, using defs and classes. It would be a lot easier. You need to you while loops.

I've written the full game for you from scratch. Here: http://codepad.org/Iy6YFW0H

Read the code and understand how it works. Feel free to use it. By the way its in Python 2.x (Sorry I don't use Python 3).

HelloUni
  • 448
  • 2
  • 5
  • 10