Python 2.7 石头、布和剪刀的改进:Python 2.7?

Python 2.7 石头、布和剪刀的改进:Python 2.7?,python-2.7,Python 2.7,我是python新手,曾尝试编写一个与上述主题相关的小项目 import random option = ["rock", "paper", "scissors"]; pc_selection = ["rock", "paper", "scissors"]; pc_move = random.choice(pc_selection) #-------------------------------------------------------------------------

我是python新手,曾尝试编写一个与上述主题相关的小项目

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()
我知道我的代码不是非常高效、最快、最聪明,所以我的问题是:

我可以修改哪一部分以使我的项目尽可能短而不丧失其功能性,即使用其他可以缩短代码长度的功能


谢谢

选项列表中的每个选项都会被其前面的选项击败。如果选择不同,则可以假设如果计算机没有在列表中选择用户选择之前的项目,则用户获胜。例如:

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()

你所说的“尽可能有效”是什么意思?易于修改?容易理解?最小空间内存?最短的时间?等等。这些是相互冲突的要求?;-/i、 规则3:好的,快的,便宜的选择2.谢谢你的澄清。我所寻找的是一个快速而好的代码,旨在尽可能消除不必要的东西。我现在觉得不太对劲的是我的代码中有太多的elif。感谢您的解释,上面的代码很简洁,并且具有相同的功能!然而,你能解释一下最后一部分的原因吗?拥有它的目的是什么?我只是添加了它来测试它。我对它进行了多次迭代,以确保它按预期工作如果它解决了你的问题,请随意接受它作为答案