在这段代码中,我试图随机创建小学年龄(10 岁)的除法问题,所以在除法时我不想要任何余数。这是我的代码,但我不确定如何做除法。

     for i in range (0,2) :
         import random
         number1 = random.randint (20,30)
         number2 = random.randint (2,5)
         answer4 = (number1 / number2)
         question4 = int(input('What is %d / %d?: ' %(number1, number2)))
         if question4 == answer4:
             print ('Well done')
             score += 1
         else:
             print ('Wrong! The answer is %d' %(answer4))
     print ('You have finished the quiz! Well done!')
     print ('Your overall score is %d out of 10' %score)

这是包含其他问题的更大测验的一部分,但我不确定这部分,因此欢迎提供任何帮助。

最佳答案

简单地将 number2 和答案随机化,然后相乘得到 number 1

number2 = random.randint(2,5)
answer4 = random.randint(20//number2, 30//number2)
number1 = number2*answer4

关于python - 如何在 Python 上进行无余数除法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34906753/

10-10 11:22