-4

Trying to build a very basic rock paper scissors code, but after adding the function it doesn't seem to work, could anyone tell me, why?

print "1 stands for paper, 2 stands for rock, 3 stand for scissors"
signs = [1, 2, 3]
gaming = 1
def game():
    from random import choice
    pc = raw_input("pick a sign, use the numbers shown above ")
    i = int(pc)
    cc = choice(signs)
    if i - cc == 0 : # 3 values
        print "it's a draw"
    elif i - cc == 1 : # 2 values
        print "you lose"
    elif  i - cc == 2 : # 1 value
        print "you win"
    elif i - cc == -1 : # 2 values
        print "you win"
    elif i - cc == -2 : # 1 value
        print "you lose"
    gamin = raw_input("if you want to play again, press 1")
    gaming = int(gamin)
while gaming == 1 :
    game
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Marsietis
  • 3
  • 3

1 Answers1

5

From what I can tell, your problem is that you are not calling game. Add () to call the function:

while gaming == 1:
    game()

However, you also need to restructure your while-loop as well as have game return gaming. Also, there are some changes you should make to improve efficiency. I rewrote your program to address all this:

# Always import at the top of your script
from random import choice
print "1 stands for paper, 2 stands for rock, 3 stand for scissors"
# Using a tuple here is actually faster than using a list
signs = 1, 2, 3
def game():
    i = int(raw_input("pick a sign, use the numbers shown above "))
    cc = choice(signs)
    if i - cc == 0:
        print "it's a draw"
    elif i - cc == 1:
        print "you lose"
    elif  i - cc == 2:
        print "you win"
    elif i - cc == -1:
        print "you win"
    elif i - cc == -2:
        print "you lose"
    return int(raw_input("if you want to play again, press 1"))
# Have the script loop until the return value of game != 1
while game() == 1:
    # pass is a do-nothing placeholder
    pass

Notice too that I got rid of a few variables. In this case, creating them didn't contribute anything positive to the script. Removing them cleans the code and improves efficiency.

  • Also, `game()` needs to return `int(gamin)`. So the while loop should actually be `gaming = game()`. – nofinator Oct 07 '13 at 19:00
  • Thanks, i'm almost ashamed, though still can't get this to work – Marsietis Oct 07 '13 at 19:02
  • Seems to work, though can't make sense of while gaming == game(), gaming has value of 1 but what value has game() ? @vik2015 didn't get errors, simply nothing would happen, though iCodez made it work – Marsietis Oct 07 '13 at 19:13
  • coming from only learning Pascal, the while cycle looks like it should do nothing, guess have to figure this one out. – Marsietis Oct 07 '13 at 19:20
  • @user2855902 - The while loop will go on continuously until the return value of `game` does not equal 1. With each iteration, `game` is called and returns a value, which is then reevaluated to see if it equals 1. If so, the loop goes again. If not, the program ends. `pass` is just a placeholder so there are no syntax errors. –  Oct 07 '13 at 19:24
  • Alright, i see, figured return was pretty much the same as print, you've been of great help. – Marsietis Oct 07 '13 at 19:29