如何将存储在变量中的两个整数相除,然后将答案存储在变量中

how to divide two integers stored in variables then store the answer in an variable

from __future__ import division
import random,time
done = False
while not done:

    num1=random.randint(1,12)
    num2=random.randint(1,4)
    (num1 % num2 == num3)
    print (int(num3))

    time.sleep(0.5)
    print("your characters atrributes are",num3+10,)
    time.sleep(0.5)

    print("Would you like to quit? Y/N or y/n")
    restart = input(">>>")
    if restart == "Y" or restart == "y":
        done = True
    elif restart == "N" or restart == "n":
        done = False
    else:
        print("invaild the program will now terminate.")
        done = True

这是我的代码,但我不知道如何将 (num1 % num2) 的结果存储到名为 num 3 的变量中,请帮助我

只需将其分配给一个新变量

num3 = num1 % num2

(num1 % num2 == num3) 是一个布尔值,说明 == 的左边是否等同于右边,然后您的代码将忽略它。

分配一个值,而不是检查是否相等使用

num3 = num1 % num2 == num3

我还注意到您的问题标题说您希望 两个数字。 请注意 / 是除法,而 % 是模数:

>>> 6/3
2
>>> 6%3
0

您混淆了 "is it equal?" 表达式和 "I want this value to be stored here" 表达式。

  • "Is it equal?"
    num3 == num1 % num2;

  • "Assign the value"
    num3 = num1 % num2;

对于计算机,每个每个 字符很重要。你需要采用同样的心态。