2016-07-12 12 views
0

私は最近、CodecademyでPythonを学んだので、最初の実際のプログラムのためにロック、ペーパー、はさみのゲームを作ることにしました。whileループ内のif文を無視するPython

プログラムをテストするとき、Pythonはwhileループ内でネストされた大きなif/elifステートメントを無視しているようです。ゲームはランダムな整数(0は岩、1は紙、2ははさみ)を生成し、それを(デバッグ用に)印刷します。次に、プレーヤーに入力を促します。その後、ifステートメントで選択肢を評価するのではなく、プレーヤーに別の選択肢を求めるだけです。また、新しいランダムな整数を出力するので、if文をスキップしてwhileループの先頭に戻るだけです。

以下はゲームのコードです。 if文に何らかの構文エラーがある場合、私はそれを見ていません。誰が何が起こっているのか分かっていますか?

from random import randint 

def choose(): 
    print '\nWill you play rock, paper, or scissors?' 
    rawhumanchoice = raw_input('> ') 
    if rawhumanchoice == 'rock' or rawhumanchoice == 'r': 
     humanchoice = 0 
    elif rawhumanchoice == 'paper' or rawhumanchoice == 'p': 
     humanchoice = 1 
    elif rawhumanchoice == 'scissors' or rawhumanchoice == 's': 
     humanchoice = 2 
    else: 
     print '\nSorry, I didn\'t catch that.' 
     choose() 

def gameinit(): 
    roundsleft = 0 
    pcwins = 0 
    humanwins = 0 

    print 'How many rounds do you want to play?' 
    roundsleft = raw_input('> ') 

    while roundsleft > 0: 
     pcchoice = randint(0,2) 
     print pcchoice 

     humanchoice = -1 
     choose() 

     if humanchoice == 0: #This is what Python ignores 
      if pcchoice == 0: 
       print '\nRock and rock... it\'s a tie!' 
       roundsleft -= 1 
      elif pcchoice == 1: 
       print '\nPaper beats rock... PC wins.' 
       roundsleft -= 1 
       pcwins += 1 
      elif pcchoice == 2: 
       print '\nRock beats scissors... human wins!' 
       roundsleft -= 1 
       humanwins += 1 
     elif humanchoice == 1: 
      if pcchoice == 0: 
       print '\nPaper beats rock... human wins!' 
       roundsleft -= 1 
       humanwins += 1 
      elif pcchoice == 1: 
       print '\nPaper and paper... it\'s a tie!' 
       roundsleft -= 1 
      elif pcchoice == 2: 
       print '\nScissors beat paper... PC wins.' 
       roundsleft -= 1 
       pcwins += 1 
     elif humanchoice == 2: 
      if pcchoice == 0: 
       print '\nRock beats scissors... PC wins.' 
       roundsleft -= 1 
       pcwins += 1 
      elif pcchoice == 1: 
       print '\nPaper beats rock... human wins!' 
       roundsleft -= 1 
       humanwins += 1 
      elif pcchoice == 2: 
       print '\nScissors and scissors... it\'s a tie!' 
       roundsleft -= 1 
    else: 
     if humanwins > pcwins: 
      result = 'The human wins the match!' 
     elif humanwins < pcwins: 
      result = 'The PC wins the match.' 
     elif humanwins == pcwins: 
      result = 'The match is a tie!' 

     print '\nThe score is %s:%s... %s \n' % (humanwins,pcwins,result) 
     gameinit() 

gameinit() 
+1

誰かが[this](http://stackoverflow.com/questions/32364127/store-function-result-into-variable)のdupとして閉じることができたら、私は感謝します。 – TigerhawkT3

+1

'choose'関数はどのように変数を変更するべきですか?それはその範囲にありません。 –

+0

'humanchoice = -1'という行を注意深く見てください。コードはif文を正しく無視します。 –

答えて

0

choose()でそれをキャッチしています。したがって、常にNoneを返します。 Noneは決して0と等しくなりません。したがって、ifステートメントはトリガーされません。

None0を比較すると、実際にはエラーになるので、これをより迅速に把握するのに役立ちます。しかし、Python 2でも、choose()を呼び出した後の謙虚なprint humanchoiceのステートメントは、関数からNone以外何も得られていないことをすぐに明らかにしました。

choose()の末尾にreturn humanchoiceを追加します。

+0

ありがとう!私はこれについても考えなかった。 – quantummechanic

0

あなたが選択したメソッドから戻るには欠けている、と値を返しません

humanchoice = choose() 
関連する問題