4

I am supposed to write a program in python that asks the user how many multiplication questions they want, and it randomly gives them questions with values from 1 to 10. Then it spits out the percentage they got correct. My code keeps repeating the same set of numbers and it also doesn't stop at the number the user asked for. Could you tell me what's wrong?

import random
import math

gamenumber = int(input("How many probems do you want?\n"))
num_1 = random.randint(1,10)
num_2 = random.randint(1,10)

def main():

    random.seed()
    count = 0
    while count < gamenumber:
        guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))

        answer = str(num_1*num_2)
        correct = guess == answer

        if guess == answer:
            print("Correct!")
        else wrong:
            print("Sorry, the answer is", answer, ".")

        result = correct/wrong   

    print("You got ", "%.1f"%result, "of the problems.")

main()
4

7 に答える 7

4

num_1と に1 回だけ割り当てnum_2ます。それらの値は決して変わりません。あなたの数字はどのように変化しますか?さらに、 をインクリメントしないcountため、元の値は常に と比較されgamenumberます。

2 つの変数に新しい乱数を割り当て、カウンターをインクリメントする必要があります。

于 2012-09-17T02:20:08.967 に答える
4

countループでインクリメントするのを忘れて、新しい値を取得num_1num_2ません。

于 2012-09-17T02:20:21.997 に答える
2

あなたが言及した問題

私のコードは同じ数字のセットを繰り返し続けます

num_1and をnum_2(1)main関数の外側に、(2) メインwhileループの外側に設定するので、これは当然のことです。簡単な修正は次のとおりです。

    while count < gamenumber:
        num_1 = random.randint(1,10)
        num_2 = random.randint(1,10)

私のコードは、要求された番号で停止しません:

count繰り返しますが、カウンターをインクリメントしないため、当然のことです。常にcount < gamenumber. 簡単な修正は次のとおりです。

    while count < gamenumber:
        num_1 = random.randint(1,10)
        num_2 = random.randint(1,10)
        guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))
        answer = str(num_1*num_2)

        count += 1

ここで、count += 1手段add 1 to count *in place*. することもできますが、実際には必要のないcount = count + 1一時変数 ( ) を作成するため、少し効率が悪くなります。count + 1

その他の問題

  • あなたは決して定義しませんwrong
  • gamenumber関数の外で定義します。この場合は問題ではありませんが、ゲームを駆動する変数であるためgamenumber、 の引数として使用する方が簡単です。main
  • あなたresultはループで定義されています。適切な回答ごとにカウンターをインクリメントし、メイン ループの後に結果を出力することをお勧めします。
  • resultとして計算されますcorrect/wrong。とは整数であり、整数を分割することはcorrect/gamenumber浮動小数点数を分割することと同じではありません。たとえば、を与えますが、を与えます。そのため、どこかでフロートを使用する必要があります。countgamenumber2/302/float(3)0.6666666
  • パーセンテージを出力したいresult場合は、 になるはずresult=correct*100./gamenumberです。
  • gamenumber0 になりたくない場合resultは、未定義になります。

したがって、全体として、main関数は

def main(gamenumber):
    random.seed()
    count = 0
    correct = 0
    while count < gamenumber:
        num_1 = random.randint(1,10)
        num_2 = random.randint(1,10)
        guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))
        answer = str(num_1*num_2)
        count += 1

        if guess == answer:
            correct += 1
            print("Correct!")
        else wrong:
            print("Sorry, the answer is", answer, ".")

    if gamenumber > 1:
        result = correct * 100./gamenumber  

    print("You got ", "%.1f"%result, "of the problems.")
于 2012-09-17T07:50:08.370 に答える
1

私にとって最も明白な問題は、無限ループがあることです。どこにも増えませんcount

于 2012-09-17T02:20:11.297 に答える
1
  1. ループを開始する前に、質問番号を一度だけ生成しています。ユーザーに質問する前に、毎回 num_1 と num_2 を生成する必要があります。

  2. 初期化後にカウント値を実際に更新することはないため、ループは永遠に続きます。

于 2012-09-17T02:23:54.093 に答える
1
import random
import math

「問題」のスペルが間違っています

gamenumber = int(input("How many probems do you want?\n"))

これらの次の 2 行をループ内に移動します

num_1 = random.randint(1,10)
num_2 = random.randint(1,10)

def main():

    random.seed()
    count = 0
    while count < gamenumber:

ここで使用でき"What is {}x{}?".format(num1, num2)ます。

        guess = int(input("What is " + str(num_1) + "x" + str(num_2) + "."))

        answer = str(num_1*num_2)

これは正解をカウントすることになっていますか?する必要がありますcorrect += guess == answer

       correct = guess == answer

不正解の数を数えるということですか?wrong += guess != answer

        if guess == answer:
            print("Correct!")

else wrong:else: #wrongおそらく構文エラー ですか?

        else wrong:
            print("Sorry, the answer is", answer, ".")

これはパーセンテージを計算する方法ではありません。correct*100/gamenumberと dedent を使用して一致させる必要がありますprint()

        result = correct/wrong   

    print("You got ", "%.1f"%result, "of the problems.")

main()

countまた、どこにもインクリメントしていません。使うだけならもっと簡単

for count in range(gamenumber):

whileループの代わりに

于 2012-09-17T04:56:12.607 に答える
0

Python は手続き型言語です。メソッド本体のステートメントを上から下に実行します。この行:

num_1 = random.randint(1,10)

代入文です。その価値を評価するためのランダムなプロセスと同等ではありません。num_1を呼び出すことによって式を評価し、random.randint(1,10)その値を , に 1 回割り当てますnum_1

別の乱数を取得するには、random.randint への別の呼び出しを強制する必要があります。また、num_1 の値を変更するたびに、ステートメントで num_1 に値を割り当てる必要があります。

于 2012-09-17T02:42:04.807 に答える