Skip to content Skip to sidebar Skip to footer

Simple Program Rock Paper Scissors While Loop

This is a basic game just like rock paper scissor but with different names, I also apologize for the commentless code. I'm almost getting to the end of my code, but I'm having a

Solution 1:

You should move

userInput = raw_input("python, ruby, or java?:")
randomInput = random.choice(gameList)

inside the while loop so that the inputs are regenerated each time the loop is run.

Solution 2:

Instead of using a while loop in the function, just call the function again! Remove the while loop from your function.

Outside the function, you can do something like:

pythonRubyJava() # Call the function firstwhile 1:
    ans = raw_input('Do you want to play again? Y/N ')
    if ans == 'Y':
        pythonRubyJava()
    else:
        print"bye!"break# Break out of the while loop.

Solution 3:

import random
defpythonRubyJava():
    gameList = ["python","ruby","java"]
    userInput = raw_input("python, ruby, or java?:")
    randomInput = random.choice(gameList)

    print randomInput
    whileTrue:
        if userInput notin gameList:
            print"The game is over"
            [change the while boolean statement in here]
        elif userInput == randomInput:
            print"stalemate"elif userInput == "python"and randomInput == "ruby":
            print"You win!"elif userInput == "ruby"and randomInput == "java":
            print"You win!"elif userInput == "java"and randomInput == "python":
            print"You win!"elif userInput == "python"and randomInput == "java":
            print"You Lose!"elif userInput == "ruby"and randomInput == "python":
            print"You Lose!"elif userInput == "java"and randomInput == "ruby":
            print"You Lose!"  

 pythonRubyJava():

This way it would run again, and show the result.

Post a Comment for "Simple Program Rock Paper Scissors While Loop"