2017-02-12 11 views
0

私はPythonには新しく、上記のテーマに関連した少しのプロジェクトを書こうとしました。ロック、ペーパー、はさみの改善:Python 2.7?

どの部分私はそのfunctionailityを失うことなくとして最短のように私のプロジェクトを作るために修正することができ、すなわち:

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

私は私のコードは、(最速と賢い)なので、私の質問は非常に効率的ではないことを知っています私のコードの長さを短縮する可能性のある他の関数を使用していますか?

ありがとうございます!

+0

http://codereview.stackexchange.com/ – YOU

+1

「可能な限り効率的」とはどういう意味ですか?修正の容易さ?理解の容易さ?最小のスペース(メモリ)?最小の時間?これらは相互に矛盾する要件ですか? ; - すなわち、 'ルール3:良い、速い、安い - 2つを選ぶ' –

+0

ご清聴ありがとうございます。私が探しているのは、できるだけ不要なものを排除することを目的とした、速くて良いコードです。現時点で私が感じていないことは、自分のコード内にあまりにも多くのエリフがあることです。 –

答えて

0

オプションリストのすべてのオプションは、その前のオプションで殴られます。選択肢が異なる場合、コンピュータがリスト内のユーザの選択に先行する項目を選択しなかった場合に、ユーザが勝つと見なすことができる。例:

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

こんにちはuser2682863。あなたの説明をありがとう、上記のコードは簡潔であり、同じ機能を持っています!しかし、あなたは "真の"部分を持っている最後の部分の理由を説明することができますか?それを持つ意思は何ですか? –

+0

私はそれをテストするために追加しました。私はそれが期待どおりに動作することを確認するためにいくつかの繰り返しを実行しました。 - それがあなたの問題を解決するならば、答えとしてそれを受け入れるように気をつけてください – user2682863

関連する問題