Python Forum

Full Version: Rock, Paper, Scissors
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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
Great job, I will try it out. Do you have any questions?
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!!!
You should add a fool proof mechanism using Try and Except. More research here: https://docs.python.org/3/tutorial/errors.html
Thank you I'll try it. What is stdin?
STDIN is standard input, it is done in the console with input().
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
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!
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
 
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
Pages: 1 2