Jump to content

Python: Piatra, Hârtie, Foarfece ( Rock Paper Scissors )


Recommended Posts

Posted

Task: Crearea joculețului 'Piatră, Hârtie, Foarfece' sub formă de aplicație in Python.

Reguli: Piatra învinge foarfecele, Foarfecele înving Hârtia, Hârtia învinge Piatra.

 

În prima versiune jocul rulează doar o singură dată. De asemenea, daca tastați un cuvant greșit sau dacă apăsați tasta "enter" înainte sa introduceți elementul, jocul se oprește.

În a doua versiune jocul rulează până când doriți dumneavoastră. De fiecare dată când câștigați sau pierdeți jocul repornește pentru o nouă rundă. Atunci când apăsați tasta "enter" sau când introduceti orice caractere care nu au legatura cu elementele, jocul se oprește.

 

# Aplicatia este codata in limba engleza.

 

v1:

Spoiler

# Rock-paper-scissors
import random
import sys

list = ["rock", "paper", "scissors"]

element = random.randrange(0, len(list))
random_word = str(list[element])
chosen_word= input("Rock, Paper, Scissors? ")

def compare():
    if chosen_word == random_word:
        return("Round draw!")
    else:
        if chosen_word == 'paper':
            if random_word == 'rock':
                return("You win! Paper wins!")
            else:
                return("You lost! The computer chose Scissors!")
        if chosen_word == 'rock':
            if random_word == 'paper':
                return("You lost! The computer chose Paper!")
            else:
                return("You win! Rock wins!")
        if chosen_word == 'scissors':
            if random_word == 'paper':
                return("You win! Scissors wins!")
            else:
                return("You lost! The computer chose Rock!")
        else:
            return("Wrong answer!")
            sys.exit()

print(compare())

 

v2:

Spoiler

# Rock-paper-scissors
import random

list = ["rock", "paper", "scissors"]

def compare(random_word,chosen_word):
    if chosen_word == random_word:
        return("Round draw!")
    else:
        if chosen_word == 'paper':
            if random_word == 'rock':
                return("You win! Paper wins!")
            else:
                return("You lost! The computer chose Scissors!")
        if chosen_word == 'rock':
            if random_word == 'paper':
                return("You lost! The computer chose Paper!")
            else:
                return("You win! Rock wins!")
        if chosen_word == 'scissors':
            if random_word == 'paper':
                return("You win! Scissors wins!")
            else:
                return("You lost! The computer chose Rock!")


while True:
    element = random.randrange(0, len(list))
    random_word = str(list[element])
    chosen_word= input("Rock, Paper, Scissors? ")
    if chosen_word == 'rock' or chosen_word == 'paper' or chosen_word == 'scissors':
        print(compare(random_word, chosen_word))
    else:
        print("Wrong answer!")
        break;

 

rock-paper-scissors.png

 

rock-paper-scissors-v1.py

rock-paper-scissors-v2.py

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...