Skip to main content
Tweeted twitter.com/StackCodeReview/status/697866460635463680
deleted 58 characters in body; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Rock-Paper-Scissors Game - Learninggame for learning purposes

JustI just want your opinions about my code. How could I improve it? Is there a better method to do it? I'm learning python and I appreciate your opinions, thanks.

Rock-Paper-Scissors Game - Learning purposes

Just want your opinions about my code. How could I improve it? Is there a better method to do it? I'm learning python and I appreciate your opinions, thanks.

Rock-Paper-Scissors game for learning purposes

I just want your opinions about my code. How could I improve it? Is there a better method to do it?

Rollback to Revision 3
Source Link
Vogel612
  • 25.3k
  • 7
  • 58
  • 141

Updated code using the provided tips:

I have updated my code, I have used a little bit of all of your answers. I appreciate your help a lot! You helped me understand better dictionaires and other concepts. Thanks very much.

from sys import exit
from random import randint

print "Welcome to the PIN Game 1!"
print "Tell me your name"

user = raw_input('> ')
print "%s, choose one of the options: " % user

#Constants
ROCK = 1
PAPER = 2
SCISSORS = 3

names = {ROCK: "ROCK", PAPER: "PAPER", SCISSORS: "SCISSORS"}

#asks the user for an entry and prints what he has chosen
def start(): 
   print """
   1. ROCK
   2. PAPER
   3. SCISSORS
   """
   
   choice = user_decision()
   cpu_choice = cpu_decision()
   result(choice, cpu_choice)
#user input
def user_decision():
    choice = 0
    
    while choice <= 0 or choice >=4:
    
        choice = int(raw_input('> '))
        if choice >= 1 or choice <= 3:
            print "You chose %s" % names[choice]
        else:
            print "What is your problem? Try again, this time choose a number between 1 and 3!"
    return choice
            
#random cpu input
def cpu_decision():
    choice = randint(1,3)
    print "Your opponent chose %s!" % names[choice]
    return choice

lose = "Sorry, you lost the match..."
win = "Congratulations, you won!!"
    
outcomes = {
    ROCK: {PAPER: lose, SCISSORS: win},
    PAPER: {ROCK: win, SCISSORS: lose},
    SCISSORS: {PAPER: win, ROCK: lose}
}

#prints the result
def result(user, comp):
    if user == comp:
        print "Gee! You tied"
    else:
        print outcomes[user][comp]

#ask if the user wants to play again
def main():
    while True:
        start()
        print """
    Wanna play again?
    1 for YES
    Anything else to quit
        """

        again1 = int(raw_input('> '))

        if again1 != 1:
            break

main()

Updated code using the provided tips:

I have updated my code, I have used a little bit of all of your answers. I appreciate your help a lot! You helped me understand better dictionaires and other concepts. Thanks very much.

from sys import exit
from random import randint

print "Welcome to the PIN Game 1!"
print "Tell me your name"

user = raw_input('> ')
print "%s, choose one of the options: " % user

#Constants
ROCK = 1
PAPER = 2
SCISSORS = 3

names = {ROCK: "ROCK", PAPER: "PAPER", SCISSORS: "SCISSORS"}

#asks the user for an entry and prints what he has chosen
def start(): 
   print """
   1. ROCK
   2. PAPER
   3. SCISSORS
   """
   
   choice = user_decision()
   cpu_choice = cpu_decision()
   result(choice, cpu_choice)
#user input
def user_decision():
    choice = 0
    
    while choice <= 0 or choice >=4:
    
        choice = int(raw_input('> '))
        if choice >= 1 or choice <= 3:
            print "You chose %s" % names[choice]
        else:
            print "What is your problem? Try again, this time choose a number between 1 and 3!"
    return choice
            
#random cpu input
def cpu_decision():
    choice = randint(1,3)
    print "Your opponent chose %s!" % names[choice]
    return choice

lose = "Sorry, you lost the match..."
win = "Congratulations, you won!!"
    
outcomes = {
    ROCK: {PAPER: lose, SCISSORS: win},
    PAPER: {ROCK: win, SCISSORS: lose},
    SCISSORS: {PAPER: win, ROCK: lose}
}

#prints the result
def result(user, comp):
    if user == comp:
        print "Gee! You tied"
    else:
        print outcomes[user][comp]

#ask if the user wants to play again
def main():
    while True:
        start()
        print """
    Wanna play again?
    1 for YES
    Anything else to quit
        """

        again1 = int(raw_input('> '))

        if again1 != 1:
            break

main()
added 1918 characters in body
Source Link

Updated code using the provided tips:

I have updated my code, I have used a little bit of all of your answers. I appreciate your help a lot! You helped me understand better dictionaires and other concepts. Thanks very much.

from sys import exit
from random import randint

print "Welcome to the PIN Game 1!"
print "Tell me your name"

user = raw_input('> ')
print "%s, choose one of the options: " % user

#Constants
ROCK = 1
PAPER = 2
SCISSORS = 3

names = {ROCK: "ROCK", PAPER: "PAPER", SCISSORS: "SCISSORS"}

#asks the user for an entry and prints what he has chosen
def start(): 
   print """
   1. ROCK
   2. PAPER
   3. SCISSORS
   """
   
   choice = user_decision()
   cpu_choice = cpu_decision()
   result(choice, cpu_choice)
#user input
def user_decision():
    choice = 0
    
    while choice <= 0 or choice >=4:
    
        choice = int(raw_input('> '))
        if choice >= 1 or choice <= 3:
            print "You chose %s" % names[choice]
        else:
            print "What is your problem? Try again, this time choose a number between 1 and 3!"
    return choice
            
#random cpu input
def cpu_decision():
    choice = randint(1,3)
    print "Your opponent chose %s!" % names[choice]
    return choice

lose = "Sorry, you lost the match..."
win = "Congratulations, you won!!"
    
outcomes = {
    ROCK: {PAPER: lose, SCISSORS: win},
    PAPER: {ROCK: win, SCISSORS: lose},
    SCISSORS: {PAPER: win, ROCK: lose}
}

#prints the result
def result(user, comp):
    if user == comp:
        print "Gee! You tied"
    else:
        print outcomes[user][comp]

#ask if the user wants to play again
def main():
    while True:
        start()
        print """
    Wanna play again?
    1 for YES
    Anything else to quit
        """

        again1 = int(raw_input('> '))

        if again1 != 1:
            break

main()

Updated code using the provided tips:

I have updated my code, I have used a little bit of all of your answers. I appreciate your help a lot! You helped me understand better dictionaires and other concepts. Thanks very much.

from sys import exit
from random import randint

print "Welcome to the PIN Game 1!"
print "Tell me your name"

user = raw_input('> ')
print "%s, choose one of the options: " % user

#Constants
ROCK = 1
PAPER = 2
SCISSORS = 3

names = {ROCK: "ROCK", PAPER: "PAPER", SCISSORS: "SCISSORS"}

#asks the user for an entry and prints what he has chosen
def start(): 
   print """
   1. ROCK
   2. PAPER
   3. SCISSORS
   """
   
   choice = user_decision()
   cpu_choice = cpu_decision()
   result(choice, cpu_choice)
#user input
def user_decision():
    choice = 0
    
    while choice <= 0 or choice >=4:
    
        choice = int(raw_input('> '))
        if choice >= 1 or choice <= 3:
            print "You chose %s" % names[choice]
        else:
            print "What is your problem? Try again, this time choose a number between 1 and 3!"
    return choice
            
#random cpu input
def cpu_decision():
    choice = randint(1,3)
    print "Your opponent chose %s!" % names[choice]
    return choice

lose = "Sorry, you lost the match..."
win = "Congratulations, you won!!"
    
outcomes = {
    ROCK: {PAPER: lose, SCISSORS: win},
    PAPER: {ROCK: win, SCISSORS: lose},
    SCISSORS: {PAPER: win, ROCK: lose}
}

#prints the result
def result(user, comp):
    if user == comp:
        print "Gee! You tied"
    else:
        print outcomes[user][comp]

#ask if the user wants to play again
def main():
    while True:
        start()
        print """
    Wanna play again?
    1 for YES
    Anything else to quit
        """

        again1 = int(raw_input('> '))

        if again1 != 1:
            break

main()
edited tags
Link
SuperBiasedMan
  • 13.1k
  • 5
  • 36
  • 62
Loading
edited tags
Link
Pimgd
  • 22.3k
  • 5
  • 66
  • 144
Loading
Source Link
Loading