Python Forum
Rock, Paper, Scissors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock, Paper, Scissors
#1
I'm new at python and in a course created this Rock, Paper, Scissors game after some hard work Smile:

import random
points = 0
choices = ['Rock', 'Paper', 'Scissors']
while True:
    comp_guess = random.randint(0,2)
    user_guess = int(input("Enter a number from 0 to 2 (0 = Rock, 1 = Paper, 2 = Scissors): "))

    print("Your guess is: " + str(choices[user_guess]) + " and my guess is: " + str(choices[comp_guess]))

    if comp_guess == user_guess:
        print("DRAW")
    elif user_guess == 0:
        if comp_guess == 1:
            print("YOU LOSE")
            points = points - 1
        else:
            print("YOU WIN")
            points = points + 1
    elif user_guess == 1:
        if comp_guess == 0:
            print("YOU WIN")
        else:
            print("YOU LOSE")
            points = points - 1
    elif user_guess == 2:
        if comp_guess == 1:
            print("YOU WIN")
            points = points + 1
        else:
            print("YOU LOSE")
            points = points - 1
    
    print("Your current score is: " + str(points))
    if points == 10:
        print("You win!")
        break
Reply
#2
Great job, I will try it out. Do you have any questions?
Reply
#3
Hello, I do not have any questions yet but as I'm still learning python I'll soon have plenty. I really appreciate your response, thanks!!!
Reply
#4
You should add a fool proof mechanism using Try and Except. More research here: https://docs.python.org/3/tutorial/errors.html
Reply
#5
Thank you I'll try it. What is stdin?
Reply
#6
STDIN is standard input, it is done in the console with input().
Reply
#7
is this better:

import random
points = 0
choices = ['Rock', 'Paper', 'Scissors']
print("Enter a number from 0 to 2 (0 = Rock, 1 = Paper, 2 = Scissors): ")
while True:
    comp_guess = random.randint(0,2)
    
    while True:
        try:
            user_guess = int(input())
            break
        except ValueError:
            print("Oops!  That was no valid number.  Try again...")
    
    

    print("Your guess is: " + str(choices[user_guess]) + " and my guess is: " + str(choices[comp_guess]))

    if comp_guess == user_guess:
        print("DRAW")
    elif user_guess == 0:
        if comp_guess == 1:
            print("YOU LOSE")
            points = points - 1
        else:
            print("YOU WIN")
            points = points + 1
    elif user_guess == 1:
        if comp_guess == 0:
            print("YOU WIN")
        else:
            print("YOU LOSE")
            points = points - 1
    elif user_guess == 2:
        if comp_guess == 1:
            print("YOU WIN")
            points = points + 1
        else:
            print("YOU LOSE")
            points = points - 1
    
    print("Your current score is: " + str(points))
    if points == 10:
        print("You win!")
        break
Reply
#8
Much better. Though when I enter a number that is out of range it tells gives me an error. Try using str(user_guess) to convert the user input, then use try and except. But you are very smart and a quick learner. Good job!
Reply
#9
Is this better?
import random
points = 0
choices = ['Rock', 'Paper', 'Scissors']
print("Enter a number from 0 to 2 (0 = Rock, 1 = Paper, 2 = Scissors): ")
while True:
    comp_guess = random.randint(0,2)
    
    while True:
        try:
            user_guess = int(input())
            if user_guess > 2:
                print("Enter a smaller number.")
                break
            elif user_guess < 0:
                print("Enter a bigger number")
                break
            break
        except ValueError:
            print("Oops!  That was no valid number.  Try again...")
    
    

    print("Your guess is: " + str(choices[user_guess]) + " and my guess is: " + str(choices[comp_guess]))

    if comp_guess == user_guess:
        print("DRAW")
    elif user_guess == 0:
        if comp_guess == 1:
            print("YOU LOSE")
            points = points - 1
        else:
            print("YOU WIN")
            points = points + 1
    elif user_guess == 1:
        if comp_guess == 0:
            print("YOU WIN")
        else:
            print("YOU LOSE")
            points = points - 1
    elif user_guess == 2:
        if comp_guess == 1:
            print("YOU WIN")
            points = points + 1
        else:
            print("YOU LOSE")
            points = points - 1
    
    print("Your current score is: " + str(points))
    if points == 10:
        print("You win!")
        break
 
Reply
#10
On line 10 user_guess = int(input()), instead of using int(input()), use str(input()) and change if user_guess == 1 etc to if user_guess == "1" etc
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Another Rock, Paper, Scissors Yoriz 4 3,131 Jun-30-2020, 07:56 PM
Last Post: Yoriz
  The tkinter version of Rock Paper Scissors menator01 3 3,149 Jun-28-2020, 07:15 AM
Last Post: ndc85430
  My version of Rock Paper Scissors menator01 12 6,036 Jun-27-2020, 10:25 PM
Last Post: menator01
  PyQt5 Version of Rock, Paper, & Scissors menator01 8 3,622 Jun-06-2020, 12:15 PM
Last Post: pyzyx3qwerty
  Rock Paper Scissor GAME inamullah9 3 3,235 Aug-11-2019, 12:17 PM
Last Post: ichabod801
  A CLI based Rock,Paper or Scissor game. Ablazesphere 7 4,431 Oct-28-2018, 07:25 AM
Last Post: Ablazesphere
  A basic Rock-paper-scissors game by me... Unlimiter 0 2,459 Dec-25-2017, 03:41 PM
Last Post: Unlimiter
  Basic Rock, Paper, Scissors CinnamonBeard 1 3,529 Dec-19-2017, 02:32 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020