2017-11-12 7 views
1

私は算数のスキルをテストするプログラムを書いています。 ランダムな質問を生成し、ランダムな操作をすることになっています。 ここに私のコードです:プログラムは予期せぬ出力を出します - なぜですか?

import random 

score = 0 
counter = 0 
ops = ['+', '-', '*', '/'] 

def question(): 
    num1 = random.randint(0,10) 
    num2 = random.randint(1,10) #Starts from 1 to avoid zerodivision error 
    operation = random.choice(ops) 
    question = float(input(f"What is {num1} {operation} {num2}?: ")) 
    global answer 
    answer = eval(str(num1) + operation + str(num2)) 
    global counter 
    counter = counter + 1 


def points(): 
    while counter < 10: 
     question() 
     if question == answer: 
      print("\nCorrect!\n") 
      global score 
      score = score + 1 
     else: 
      print(f"\nWrong! The answer is {answer}\n") 


points() 

print(f"\nYou got {score} out of {counter}") 

しかし、それは、この出力できます:入力が答えと一致した場合に正しいと言う、とスコアをカウントし、外のスコアを印刷するようになっている

What is 9 + 3?: 12 
Wrong! The answer is 12 

を最後は10です。

これを解決してください。

答えて

3

問題は、その行で

if question == answer: 

であり、questionは、前に定義された機能question()への参照です。 answerは、質問に対する答えを保持するintなので、==は常にFalseです。あなたは、実際にユーザーが入力した内容を知ることはありません

をその問題を解決するには、このような何かを実行します。

def question(): 
    num1 = random.randint(0,10) 
    num2 = random.randint(1,10) #Starts from 1 to avoid zerodivision error 
    operation = random.choice(ops) 
    user_answer = float(input(f"What is {num1} {operation} {num2}?: ")) 
    global answer 
    answer = eval(str(num1) + operation + str(num2)) 
    global counter 
    counter = counter + 1 

    return user_answer 

def points(): 
    while counter < 10: 
     user_answer = question() 
     if user_answer == answer: 
      print("\nCorrect!\n") 
      global score 
      score = score + 1 
     else: 
      print(f"\nWrong! The answer is {answer}\n") 

私は何が起こっていることをより明確にするために名前を変更しました。


補足として、グローバル変数を使用しないことを強く推奨します。彼らはほとんど決して必要ではなく、一般的に問題を混乱させる。たとえば、私はそうだと思います

def question(): 
    num1 = random.randint(0,10) 
    num2 = random.randint(1,10) #Starts from 1 to avoid zerodivision error 
    operation = random.choice(ops) 

    user_answer = float(input(f"What is {num1} {operation} {num2}?: ")) 
    real_answer = eval(str(num1) + operation + str(num2)) 

    return user_answer, real_answer 

def points(): 
    score = 0 
    for questions_asked in range(1, 11): 
     user_answer, real_answer = question() 
     if user_answer == real_answer: 
      print("\nCorrect!\n") 
      score += 1 
     else: 
      print(f"\nWrong! The answer is {answer}\n") 

これは、プログラムの流れを理解することがはるかに簡単になります。

+0

ここでは、user_answer、real_answer = question()という行に何がありますか? –

+0

@LaveenChandnani [タプルの展開](https://en.wikibooks.org/wiki/Python_Programming/Tuples#Packing_and_Unpacking)です。 –

+0

それで、user_answerとreal_answerをquestion()関数に代入していますか?私は、質問の手続きがどこで呼び出されたか分かりません。 –

1

あなたの質問はすでにJosh Karpelによって回答されていますが、私は分割に関する別の問題を指摘したいと思っていました。それは"2.0""2"の糸比較をやっているので、ユーザは、スクリプトは答えが間違っていると言うだろう... 4/22であると言うので、もし

部門は常に、代わりにintfloatになります。そのため、Joshのバージョンでは、ユーザーの回答をfloatに変換しています。このように、答えは数値的に比較されます。

さらに、スクリプトは現在、回答が難しいWhat is 4/7 ?のようなものを要求することができます。 1つの回避策は、num1で均等に割り切れるまでnum2をランダム化することです。

def question(): 
    num1 = random.randint(0, 10) 
    num2 = random.randint(1, 10) 
    operation = random.choice(ops) 
    if operation == '/': 
     while num1 % num2 != 0: 
      num2 = random.randint(1, 10) 
    # do eval 
+0

私のためにいくつかを明確にしているああ。ありがとうございました –

関連する問題