Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)

Hello, all. I'm totally new to python. I'm trying to type a small project related to the above subject

import random

option = ["rock", "paper", "scissors"];   

pc_selection = ["rock", "paper", "scissors"];  

pc_move = random.choice(pc_selection)

#-------------------------------------------------------------------------------

def first_condition():

    select = raw_input("Please select your choice\n") 

    print "Your choice:", select

    if select in option:

        pc_move

        print "Computer choice:", pc_move

    else:

        first_condition() 

    if select == pc_move:

        print "Result: draw"

    elif select == "rock" and pc_move == "paper":

        print "Result: Computer wins"

    elif select == "paper" and pc_move == "scissors":

        print "Result: Computer wins"

    elif select == "scissors" and pc_move == "rock":

        print "Result: Computer wins"

    elif select == "rock" and pc_move == "scissors":

        print "Result: You win"

    elif select == "paper" and pc_move == "rock":

        print "Result: You win"

    elif select == "scissors" and pc_move == "paper":

        print "Result: You win"

first_condition()

I realized that my code aren't exceptionally effective (quickest and cleverest), so my inquiry is: 

Which part might I be able to correct to make my undertaking as most limited as conceivable without losing its functionality, for example utilizing different functions that could lessen the length of my code?

Thanks

1 Answer

0 votes
by (26.4k points)

Each alternative in the choices list is beaten by the choice that goes before it. In the event that the decisions are unique, at that point, it very well may be expected that the user wins if the PC didn't pick the item that goes before the client's decision in the list. For example:

import random

option = ["scissors", "paper", "rock"] # I reversed the original list

#----------------------------------------------------------------------

def first_condition():

    pc_move = random.choice(option) # there only needs to be 1 option list

    select = raw_input("Please select your choice\n") 

    print "Your choice:", select

    if select in option:

        print "Computer choice:", pc_move

    else:

        return first_condition() 

    if pc_move == select:

        print("Draw")

        return

    # find the index of the user's choice

    index = option.index(select) 

    # did the pc choose the item before this one?       

    you_win = option[index-1] != pc_move

    print("You %s" % ("win" if you_win else "lose"))

while True:

    print("-"*50)

    first_condition()

Want to learn Python to get expertise in the concepts of python? Join Python certification course and get certified

Related questions

0 votes
1 answer
0 votes
4 answers
0 votes
4 answers

Browse Categories

...