私は算数のスキルをテストするプログラムを書いています。 ランダムな質問を生成し、ランダムな操作をすることになっています。 ここに私のコードです:プログラムは予期せぬ出力を出します - なぜですか?
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です。
これを解決してください。
ここでは、user_answer、real_answer = question()という行に何がありますか? –
@LaveenChandnani [タプルの展開](https://en.wikibooks.org/wiki/Python_Programming/Tuples#Packing_and_Unpacking)です。 –
それで、user_answerとreal_answerをquestion()関数に代入していますか?私は、質問の手続きがどこで呼び出されたか分かりません。 –