Rock,paper & scissor game


#1

I’m trying to put the number of choice and the noun of choice in the same list but it doesn’t work.
please check my code :blush:

answer="y"

player1_score=0

player2_score=0

list=[[1,'rock'],[2,'scissor'],[3,'paper']]

while answer =="y": # while answer is yes wa can play

   for elt in enumerate(list): # enumerat the list of choice
   
       choice_player1=input("player1 enter your choice 1,2 or 3\n") # choice of player1
   
       choice_player2=input("player2 enter your choice 1,2 or 3\n") # choice of player2
   
       try: # treatment of exception
   
           if choice_player1 == choice_player2:
           
               raise ValuError("your choices are equivalent try again")
           
           #except ValuError:
       
           #print("equivalent value betwwen player") # end of the exception
       
           else: # begin of the game
       
               if choice_player1 == list[1] and choice_player2 == list[2]:
                   player1_score=1
               if choice_player1 == list[1] and choice_player2 == list[3]:
                   player2_score=1
               if choice_player1 == list[2] and choice_player2 == list[1]:
                   player2_score=1
               if choice_player1 == list[2] and choice_player2 == list[3]:
                   player1_score=1
               if choice_player1 == list[3] and choice_player2 == list[1]:
                   player1_score=1
               if choice_player1 == list[3] and choice_player2 == list[2]:
                   player2_score=1
            
           # treatment of result of the game
           
               if player1_score == player2_score: 
           
                   print ("draw" + str(player1_score)+"vs"+str(player2_score)
               
               elif player1_score > player2_score:
               
                   print (" player 1 won" + str(player1_score)+"vs"+str(player2_score))
               
               else:
               
                   print ("player 2 won" + str(player2_score)+"vs"+str(player1_score))
           
           # treatment of exiting the game or playing again
           
           answer = input("if you want playing again press y else press n")
           
           if answer == "y":
               
               break
           
           else:
               
               lettre = input("press q to exit")
               
               if lettre == "q":
                   
                   print ("Game over")
           
           

#2

I’m working on it, and I’m doing some changes, so I’m at last,and program don’t accept the elif


#3

I am still starting with python basics,…
Such a good start from you…
keep up :rose:


#4

it’s just an idea, but it doesn’t work yet, I must study hard again


#5

عمل جميل جداً !

قمت بتنظيف الكود قليلاً, وتفكيك القطع في البرنامج إلى أجزاء أصغر يسهل ملاحظة الأخطاء فيها, ما زال لا يعمل… لكن متأكد أنه بإمكانك إصلاح الخطأ:

answer = "y"

player1_score = 0
player2_score = 0

choices = [[1, 'rock'], [2, 'scissor'], [3, 'paper']]


def get_player_choice():
    choice_player1 = input(
        "player1 enter your choice 1,2 or 3\n")  # choice of player1

    choice_player2 = input(
        "player2 enter your choice 1,2 or 3\n")  # choice of player2

    return (choice_player1, choice_player2)


def start_match(choice_player1, choice_player2):

    if choice_player1 == choice_player2:
        raise ValueError("your choices are equivalent try again")

    else:  # begin of the game

        if choice_player1 == choices[1] and choice_player2 == choices[2]:
            player1_score = 1
        if choice_player1 == choices[1] and choice_player2 == choices[3]:
            player2_score = 1
        if choice_player1 == choices[2] and choice_player2 == choices[1]:
            player2_score = 1
        if choice_player1 == choices[2] and choice_player2 == choices[3]:
            player1_score = 1
        if choice_player1 == choices[3] and choice_player2 == choices[1]:
            player1_score = 1
        if choice_player1 == choices[3] and choice_player2 == choices[2]:
            player2_score = 1


def check_who_wins():
    if player1_score == player2_score:
        print("draw" + str(player1_score)+"vs"+str(player2_score))

    elif player1_score > player2_score:
        print(" player 1 won" + str(player1_score) +
              "vs"+str(player2_score))

    else:
        print("player 2 won" + str(player2_score) +
              "vs"+str(player1_score))


while answer == "y":  # while answer is yes wa can play

    try:

        (player1_choice, player2_choice) = get_player_choice()
        start_match(player1_choice, player2_choice)
        check_who_wins()

        answer = input("if you want playing again press y else press n")

        if answer == "y":
            break

        else:
            lettre = input("press q to exit")
            if lettre == "q":
                print("Game over")

    except:
        pass


#6

thank you so much sir @YaserAlnajjar :star_struck: I’ll see that


#7

In Sololearn it gives me an error : time limit exceeded.
I’m doing some changes and It works on Pycharm :star_struck:
But it gives me usually a result of : draw … :grin:
I think that the procedure : start_match can’t read the list of choice :thinking:
check it please !

answer = "n"

player1_score = 0
player2_score = 0

player1_choice = " "
player2_choice = " "
choice = ['rock','paper','scissor']
while not(player1_choice in choice):
    player1_choice = raw_input("player1 enter your choice."+str(choice))
while not(player2_choice in choice):
    player2_choice = raw_input("player2 enter your choice."+ str(choice))

def start_match(player1_choice, player2_choice):

    if player1_choice == player2_choice:

        raise ValueError("your choices are equivalent try again")

    else:  # begin of the game

        if player1_choice == choice[1] and player2_choice == choice[2]:
            player1_score += 1

        if player1_choice == choice[1] and player2_choice == choice[3]:
            player2_score += 1

        if player1_choice == choice[2] and player2_choice == choice[1]:
            player2_score += 1

        if player1_choice == choice[2] and player2_choice == choice[3]:
            player1_score += 1

        if player1_choice == choice[3] and player2_choice == choice[1]:
            player1_score += 1

        if player1_choice == choice[3] and player2_choice == choice[2]:
            player2_score += 1

#def check_who_wins():
if player1_score == player2_score:
    print("draw " + str(player1_score)+" vs "+str(player2_score))

elif player1_score > player2_score:
    print(" player 1 won " + str(player1_score) +" vs "+str(player2_score))

else:
    print("player 2 won " + str(player2_score) +" vs "+str(player1_score))

while answer == "y":  # while answer is yes we  can play

    try:

        start_match(player1_choice, player2_choice)

        answer = raw_input("if you want playing again press y else press n")

        if answer == "y":
            break

        else:
            lettre = input("press q to exit")
            if lettre == "q":
                print("Game over")

    except:
        pass


#8

يبدو أن المشكلة في دالة start_match… ما رأيك بالإطلاع على هذا الحل:

يوجد فيه طريقة جميلة جداً, مثلاً الأرقام تقابلها الاختيارات (تسمى هذا النوع من البيانات dictionary):

    choices = {1 : 'Rock', 2 : 'Paper', 3 : 'Scissors'}

الفرق بينها وبين list أنها محاطة بـ {} بدلاً من []

وبعدين المقارنة تتم بهذا الشكل:

def compare(playerChoice,cpuChoice):
    results = {('Paper','Rock') : True,
               ('Paper','Scissors') : False,
               ('Rock','Paper') : False,
               ('Rock','Scissors') : True,
               ('Scissors','Paper') : True,
               ('Scissors','Rock') : False}
    return results[(playerChoice,cpuChoice)]

يدخل tuple, مثلاً rock & paper, وكل واحد يقابله true أو false يحدد من الفائز (الكمبيوتر أم اللاعب)


#9

wonderful :heart_eyes:
هذا ما أبحث عنه في عقلي :dizzy_face:
لم أتعمق بعد في هذا
اليوم ان شاء الله سأبدأ في دراسة
POO with python