2017-10-31 7 views
2

私はPythonには新しく、コンピュータが特定の範囲内の乱数を生成し、それが選択した数を推測しようとするプログラムを作っています。私はコンピュータがforループを使って複数の数字(この場合は2)を推測させるようにしています。私はまた、ループの各反復後に取った推測の数を追跡しています。その後、ループが終了した後で、それらの推測を​​平均するつもりです。しかし、私は平均を取得しようとすると印刷されません。コードに何が問題なのか分かりません。誰かがこの問題で私を助けることができますか?forループの後に平均値を出力するにはどうすればよいですか?

import random 

def guess(): 
     the_num = random.randint(1, 100) 
     print('The number to guess is',the_num) 
     comp_guess = random.randint(1, 100) 
     print('The computer guesses ', comp_guess) 
     tries = 1 

     while comp_guess != the_num: 
      print('The computer guesses ', comp_guess) 
      tries += 1 
      if comp_guess == the_num: 
       break 
      else: 
       comp_guess = random.randint(1, 100) 

     for i in range(2): 
      guess() 

     print('The computer took',tries,'guesses') 
     print('The computer guessed it right!') 
     print('The computer guessed',(tries/2),'times on average') 
     # Trying to calculate the average number of guesses after the loop is finished 
+4

あなたがいない基本ケースで、あなたの関数の内部で ')('推測を呼んでいますまたはインデントのエラーですか?あなたのコードを実行すると、最大再帰エラーが発生します – 0TTT0

+0

最初に、定義しただけなので、関数(つまり、 'guess()')を呼び出します。次に、範囲(2)内のfor for:guess()を削除するか、永遠に実行します。 – ryugie

+0

@ 0TTT0私はそれを実行すると、大丈夫です。私は実際に何を言っているのか分からない。そして、はい、私は私の機能の中で推測を呼んでいました。 – LLLL90

答えて

0

guess()関数は、現在コーディングされている方法が返ってこないので、そのように呼び出す必要はありません。ここで

は、私はそれをする必要性を避けるために、あなたのコードを再編成する方法をです:

import random 

NUM_GUESSES = 2 

def guess(): 
    the_num = random.randint(1, 100) 
    print('The number to guess is', the_num) 

    tries = 0 
    comp_guess = None 
    while comp_guess != the_num: 
     comp_guess = random.randint(1, 100) 
     print('The computer guesses', comp_guess) 
     tries += 1 

    return tries 

total_tries = 0 
for i in range(NUM_GUESSES): 
    tries = guess() 
    total_tries += tries 
    print('The computer took', tries, 'guesses') 
    print('The computer guessed it right!') 

average = total_tries/NUM_GUESSES 
print('The computer guessed', average, 'times on average') 

出力例:

The number to guess is 88 
The computer guesses 58 
The computer guesses 19 
The computer guesses 55 
The computer guesses 16 
The computer guesses 85 
The computer guesses 24 
The computer guesses 76 
The computer guesses 36 
The computer guesses 18 
The computer guesses 58 
The computer guesses 27 
The computer guesses 1 
The computer guesses 2 
The computer guesses 100 
The computer guesses 74 
The computer guesses 49 
The computer guesses 26 
The computer guesses 76 
The computer guesses 28 
The computer guesses 38 
The computer guesses 27 
The computer guesses 100 
The computer guesses 7 
The computer guesses 50 
The computer guesses 54 
The computer guesses 80 
The computer guesses 36 
The computer guesses 39 
The computer guesses 61 
The computer guesses 28 
The computer guesses 11 
The computer guesses 38 
The computer guesses 27 
The computer guesses 29 
The computer guesses 70 
The computer guesses 45 
The computer guesses 18 
The computer guesses 65 
The computer guesses 40 
The computer guesses 88 
The computer took 40 guesses 
The computer guessed it right! 
The number to guess is 99 
The computer guesses 73 
The computer guesses 99 
The computer took 2 guesses 
The computer guessed it right! 
The computer guessed 21.0 times on average 
+0

助けてくれてありがとう!私は今何を修正する必要があるのか​​をよく理解しています。 – LLLL90

0

guess自体の内部を呼び出すことで、ベースケースを持たない再帰関数が作成されるため、決して終了しません。より良いアプローチは、実際のコードでループを発生させることです。次のコードでは、入力に応じて何回も呼び出すことができるようにして、関数を柔軟にするために自由を取った。

import random 

def guess(num_loops): 
    total_tries = 0 
    for i in range(num_loops): 
     the_num = random.randint(1, 100) 
     print('The number to guess is',the_num) 
     comp_guess = random.randint(1, 100) 
     print('The computer guesses ', comp_guess) 
     tries = 1 

     while comp_guess != the_num: 
      # print('The computer guesses ', comp_guess) 
      tries += 1 
      print(tries) 
      if comp_guess == the_num: 
       break 
      else: 
       comp_guess = random.randint(1, 100) 
     total_tries += tries 
     print('The computer took',tries,'guesses') 
     print('The computer guessed it right!') 
    print('The computer guessed',(total_tries/num_loops),'times on average') 
    # Trying to calculate the average number of guesses after the loop is finished 

guess(2) 
0

あなたが持っているものは、関数がそれ自身を呼び出すときの再帰です。

あなたのケースでは、再帰、それを呼び出す関数とコードが1つだけ必要はありません。

あなたの関数は1つのことを行い、うまくいくはずです。この場合、あなたの機能はコンピュータが生成した番号を推測することです。

def guess(): 
    the_num = random.randint(1, 100) 
    print('The number to guess is',the_num) 
    comp_guess = random.randint(1, 100) 
    print('The computer guesses ', comp_guess) 
    tries = 1 

    while comp_guess != the_num: 
     print('The computer guesses ', comp_guess) 
     tries += 1 
     if comp_guess == the_num: 
      break 
     else: 
      comp_guess = random.randint(1, 100) 

このコードを2回実行したいと思うようです。だからコンピュータは複数の数字を推測し、推測しようとしているそれぞれの数字に対して多くの試みをしています。

この場合、関数の外で関数を複数回呼び出すだけで済みます。

for i in range(2): 
    guess() 

このコードはインデントされておらず、機能の一部ではありません。

あなたはPythonのドキュメントに行くことができる機能を知りたいときhttps://docs.python.org/3/tutorial/controlflow.html#defining-functions

私は非常にお勧めの本にもリンクします:http://inventwithpython.com/chapter6.html

これらのリソースを使用して、関数についての詳細を学ぶのに役立ちます。

この回答があなたを助けました。ご質問がありましたら、下記のコメントをお寄せください!

関連する問題