【问题标题】:Stop the program from asking the same thing twice?阻止程序问同样的事情两次?
【发布时间】:2017-08-31 13:25:14
【问题描述】:

在 python 中使用随机方程进行数学测验。我不确定如何阻止程序两次询问相同的问题?
该程序从加、减或乘的列表中询问十个问题,每个问题都使用随机数。我已经设法使十个问题随机化,但我不确定如何阻止它两次选择相同的两个数字?例如,它会为一个问题选择 1+3,但之后它会多次询问同一个问题。代码如下:

import random
#Asks for name
name = input("What's your name?")
#Stops user from entering invalid input when entering their class
classchoices = ["A","B","C"]
classname = input("What class are you in?")
while classname not in classchoices:
    classname = input("Not a valid class, try again:")

print(name, ",", classname)
print("Begin quiz!")

questions = 0


def add(a,b):
    addQ =  int(input(str(a) + "+" + str(b) + "="))
    result = int(int(a) + int(b))
    if addQ != result:
        print ("Incorrect!", result)
    else:
        print("Correct")

a = random.randint(1,12)
b = random.randint(1,12)



def multiply(a,b):
    multQ =  int(input(str(c) + "X" + str(d) + "="))
    results = int(int(c) * int(d))
    if multQ != results:
        print ("Incorrect! The answer is", results)
    else:
        print("Correct")

c = random.randint(1,12)
d = random.randint(1,12)

def subtract(a,b):
    subQ =  int(input(str(e) + "-" + str(f) + "="))
    resultss = int(int(e) - int(f))
    if subQ != resultss:
        print ("Incorrect! The answer is", resultss)
    else:
        print("Correct")

e = random.randint(1,12)
f = random.randint(1,12)


while questions in range(10):
    Qlist = [add, subtract, multiply]
    random.choice(Qlist)(a,b)
    questions += 1
if questions == 10:
    print ("End of quiz")

任何帮助将不胜感激,谢谢。

【问题讨论】:

    标签: python python-3.x function random user-input


    【解决方案1】:

    对于您提出的每个问题,您可以将其添加到一组“asked_questions”中,并使用“in”方法测试是否已被问过,如果是,则生成一个新问题。

    不确定这是否是最有效的方法,但它确实有效,并且对于您的小应用程序来说绰绰有余。

    【讨论】:

      【解决方案2】:

      这里的问题是您在程序开始时生成随机数,但不是每次问问题时都重新生成它们:您的add 函数将始终使用abmultiply将始终使用cd,而subtract 将始终使用ef 进行所有各自的计算,其值永远不会改变。

      此外,您输入的参数对于multiplysubtract 没有任何价值,因为您只是忽略它们并分别使用c, de, f,而没有注意输入的参数。

      为了解决这两个问题,我会将随机生成的数字放在while 循环中,并让函数使用正确的输入参数进行计算。

      此外,while 迭代有点多余,而没有额外位的简单 for questions in range(10) 更直接。因此questions 变量是无用的。

      考虑到所有这些,这是重写的代码。

      import random
      #Asks for name
      name = input("What's your name?")
      #Stops user from entering invalid input when entering their class
      classchoices = ["A","B","C"]
      classname = input("What class are you in?")
      while classname not in classchoices:
          classname = input("Not a valid class, try again:")
      
      print(name, ",", classname)
      print("Begin quiz!")
      
      def add(a,b):
          addQ =  int(input(str(a) + "+" + str(b) + "="))
          result = int(int(a) + int(b))
          if addQ != result:
              print ("Incorrect!", result)
          else:
              print("Correct")
      
      def multiply(a,b):
          multQ =  int(input(str(a) + "X" + str(b) + "="))
          results = int(int(a) * int(b))
          if multQ != results:
              print ("Incorrect! The answer is", results)
          else:
              print("Correct")
      
      def subtract(a,b):
          subQ =  int(input(str(a) + "-" + str(b) + "="))
          resultss = int(int(a) - int(b))
          if subQ != resultss:
              print ("Incorrect! The answer is", resultss)
          else:
              print("Correct")
      
      
      for i in range(10):
          Qlist = [add, subtract, multiply]
          random.choice(Qlist)(random.randint(1, 12),random.randint(1, 12))
      
      print ("End of quiz")
      

      程序可以通过创建一个函数来进一步完善,该函数根据第三个参数来管理打印和检查结果,该参数指示它应该执行什么操作。

      【讨论】:

        猜你喜欢
        • 2011-01-20
        • 2021-04-22
        • 1970-01-01
        • 1970-01-01
        • 2011-12-10
        • 1970-01-01
        • 2020-12-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多