2016-07-31 6 views
-5

私はPythonを使用しています。現在、それに新しいです。私は勝利カウンターでロックペーパーハサミゲームを作っています。私はいくつかのソリューションをオンラインで見てきましたが、それは私のためには機能しません。私はそれを完了するのに近いです。しかし、それに誤りがあります。私は集計カウンターを得ることができません。プログラムの最後に数えてくださいロック、ペーパー、はさみタリーカウンタpython

from random import randint 

print ("Rock ,Paper,Scissors game.") 

#Function to get computer input 
def generate(): 
    comlist = ["rock","paper","scissors"] 
    comans = comlist[randint(-1,2)] 
    if comans == "rock": 
     print ("Computer choose rock.") 
    elif comans == "paper" : 
     print ("Computer choose paper.") 
    elif comans == "scissors": 
     print ("Computer choose scissors.") 
    return comans 

#Function to get user input 
def user(): 
    userchoice = input ("Choose rock, paper , or scissors.") 
    while userchoice != 'rock' and userchoice != 'paper' and userchoice != 'scissors': 
     print ("Invalid input. Please enter again") 
     userchoice = input ("Choose rock, paper , or scissors.") 
    if userchoice == "rock": 
     print ("You choose rock.") 
     choice = userchoice 
    elif userchoice == "paper" : 
     print ("You choose paper.") 
     choice = userchoice 
    else: 
     userchoice == "scissors" 
     print ("You choose scissors.") 
     choice = userchoice 
    return choice 

#Function to determine winner 
def result(comans ,choice): 
    global result_set 
    if choice == comans: 
     print ("Tie") 
    elif choice == "rock": 
     if computer == "paper": 
      print ("You lose") 
     else : 
      print("You win") 
      result_set ='win' 
    elif choice == "paper": 
     if computer == "scissors": 
      print("You lose") 
     else: 
      print("You win") 
      result_set ='win' 
    elif choice == "scissors": 
     if computer == "rock": 
      print("You lose") 
     else: 
      print("You win") 
      result_set ='win' 

#Function to get win taly 
def wincounter (result,guess,computer): 
    if result_set == 'win': 
     win += 1 
    else: 
     pass 
    print (win) 

#Main program 
counter = 0 
win = 0 
diffulty = input(' Please enter diffulty.("1" for easy , "2" for medium, "3" for hard)') 
while diffulty != '1' and diffulty != '2' and diffulty != '3': 
    print ('Invalid input') 
    diffulty = input(' Please enter diffulty.("1" for easy , "2" for medium, "3" for hard)') 
if diffulty == '1': 
    print ("You have choose easy") 
    counter = 1 
    guess = user() 
    computer = generate() 
    result (computer, guess) 
    while counter < 3: 
     guess = user() 
     computer = generate() 
     result (computer, guess) 
     counter +=1 
if diffulty == '2': 
    print ("You have choose medium") 
    counter = 1 
    guess = user() 
    computer = generate() 
    result (computer, guess) 
    while counter < 5: 
     guess = user() 
     computer = generate() 
     result (computer, guess) 
     counter +=1 
if diffulty == '3': 
    print ("You have choose hard") 
    counter = 1 
    guess = user() 
    computer = generate() 
    result (computer, guess) 
    while counter < 10: 
     guess = user() 
     computer = generate() 
     result (computer, guess) 
     counter +=1 

ここは私のコードです。やや厄介です。私がまだPythonで新しくなって以来、残念です。助けてくれてありがとう。 実行後。

Choose rock, paper , or scissors.rock 
You choose rock. 
Computer choose scissors. 
You win 
Choose rock, paper , or scissors.rock 
You choose rock. 
Computer choose rock. 
Tie 
Choose rock, paper , or scissors.rock 
You choose rock. 
Computer choose rock. 
Tie 

最後のラウンドの終了後に勝利結果を示すと仮定します。

+0

現在、何が問題になっていますか?何が効いていないのですか? – Mehari

+0

まず、「うまくいかない」とは何かを説明してください:クラッシュ?間違った結果?...そして、関連する部分のみを表示してください(少なくとも、少なくとも100行のコード行を読むことを好む人はいない)。 [ask]と[mcve]を参照してください。 – Julien

+0

wincounter関数 – samgak

答えて

0

私はあなたのコードを修正しました。 。 。

from random import randint 

print ("Rock ,Paper,Scissors game.") 

#Function to get computer input 
def generate(): 
    comlist = ["rock","paper","scissors"] 
    comans = comlist[randint(-1,2)] 
    if comans == "rock": 
     print ("Computer choose rock.") 
    elif comans == "paper" : 
     print ("Computer choose paper.") 
    elif comans == "scissors": 
     print ("Computer choose scissors.") 
    return comans 

#Function to get user input 
def user(): 
    userchoice = input ("Choose rock, paper , or scissors.") 
    while userchoice != 'rock' and userchoice != 'paper' and userchoice != 'scissors': 
     print ("Invalid input. Please enter again") 
     userchoice = input ("Choose rock, paper , or scissors.") 
    if userchoice == "rock": 
     print ("You choose rock.") 
     choice = userchoice 
    elif userchoice == "paper" : 
     print ("You choose paper.") 
     choice = userchoice 
    else: 
     userchoice == "scissors" 
     print ("You choose scissors.") 
     choice = userchoice 
    return choice 

#Function to determine winner 
def result(comans ,choice): 
    result_set = '' 
    if choice == comans: 
     print ("Tie") 
    elif choice == "rock": 
     if computer == "paper": 
      print ("You lose") 
     else : 
      print("You win") 
      result_set ='win' 
    elif choice == "paper": 
     if computer == "scissors": 
      print("You lose") 
     else: 
      print("You win") 
      result_set ='win' 
    elif choice == "scissors": 
     if computer == "rock": 
      print("You lose") 
     else: 
      print("You win") 
      result_set ='win' 

    wincounter(result_set) 

#Function to get win taly 
def wincounter (result): 
    global win 
    if result == 'win': 
     win += 1 
    else: 
     pass 
    #print (win) 


def print_win_count(): 
    global win 
    print ('you have win '+ str(win) + ' times') 

#Main program 
counter = 0 
win = 0 
diffulty = input(' Please enter diffulty.("1" for easy , "2" for medium, "3" for hard)') 
while diffulty != '1' and diffulty != '2' and diffulty != '3': 
    print ('Invalid input') 
    diffulty = input(' Please enter diffulty.("1" for easy , "2" for medium, "3" for hard)') 
if diffulty == '1': 
    print ("You have choose easy") 
    counter = 1 
    guess = user() 
    computer = generate() 
    result (computer, guess) 
    while counter < 3: 
     guess = user() 
     computer = generate() 
     result (computer, guess) 
     counter +=1 
    print_win_count() 
if diffulty == '2': 
    print ("You have choose medium") 
    counter = 1 
    guess = user() 
    computer = generate() 
    result (computer, guess) 
    while counter < 5: 
     guess = user() 
     computer = generate() 
     result (computer, guess) 
     counter +=1 
    print_win_count() 
if diffulty == '3': 
    print ("You have choose hard") 
    counter = 1 
    guess = user() 
    computer = generate() 
    result (computer, guess) 
    while counter < 10: 
     guess = user() 
     computer = generate() 
     result (computer, guess) 
     counter +=1 
    print_win_count(0) 
+0

ああwow..thanksたくさん –

関連する問題