【问题标题】:check if a number combination is found in a list of combinations检查是否在组合列表中找到数字组合
【发布时间】:2019-03-09 08:26:05
【问题描述】:

我正在用 Python 创建一个模拟乘法闪存卡的程序。我已经走了很远,但我不知道如何不重复数字组合。如何检查一对数字是否已经出现?

from __future__ import division
from itertools import combinations
import random
amountCorrect = 0
amountMissed = 0
comb = combinations([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 2)

print("Type 0 at any time to exit and see your score.")
while True:
  firstNumber = random.randint(1,12)
  secondNumber = random.randint(1,12)
  ans = int(input("What is " + str(firstNumber) + " x " + str(secondNumber) + ": "))
  if ans == 0:
    break
  elif ans == firstNumber * secondNumber:
    amountCorrect += 1
  else:
    amountMissed += 1

totalProblems = amountCorrect + amountMissed
percentCorrect = amountCorrect/totalProblems

if .9 < percentCorrect <= 1:
  print("Great job, you are doing awesome!")
elif .7 <= percentCorrect <= .89:
  print("You are doing well,keep it up.")
elif .5 <= percentCorrect <= .69:
  print("You are half way to becoming a master.")
else:
  print("Keeping practicing, you will be a master one day.")

【问题讨论】:

    标签: python python-2.7 list conditional combinations


    【解决方案1】:

    简而言之,使用集合来存储您已经使用过的数字对。这是一些代码。您从不在代码中使用 combinations,所以我将其删除。

    from __future__ import division
    import random
    amountCorrect = 0
    amountMissed = 0
    highestNumber = 12
    
    print("Type 0 at any time to exit and see your score.")
    used = set()
    while True:
      if len(used) == highestNumber ** 2:
          break
      while True:
        firstNumber = random.randint(1,highestNumber)
        secondNumber = random.randint(1,highestNumber)
        pair = (firstNumber, secondNumber)
        if pair not in used:
          used.add(pair)
          break
      ans = int(input("What is " + str(firstNumber) + " x " + str(secondNumber) + ": "))
      if ans == 0:
        break
      elif ans == firstNumber * secondNumber:
        amountCorrect += 1
      else:
        amountMissed += 1
    
    totalProblems = amountCorrect + amountMissed
    percentCorrect = amountCorrect/totalProblems
    
    if .9 < percentCorrect <= 1:
      print("Great job, you are doing awesome!")
    elif .7 <= percentCorrect <= .89:
      print("You are doing well,keep it up.")
    elif .5 <= percentCorrect <= .69:
      print("You are half way to becoming a master.")
    else:
      print("Keeping practicing, you will be a master one day.")
    

    我刚刚创建了一个名为used 的空集,并添加了一个新的内部循环。该循环测试这对数字是否已被使用。如果是这样,它只是再次循环并尝试一对新的数字。我还添加了一个变量来存储可能的最高数字,并且used 集的测试已满。如果它已满,我会结束测验。没有这个,当所有的可能性都被尝试时,程序将进入一个无限循环。

    请注意,此代码将允许1,22,1。如果您只想允许其中一个,请将(firstNumber, secondNumber)(secondNumber, firstNumber) 添加到used 集合中。

    【讨论】:

    • 您好,Rory,谢谢您的回复。我应该澄清一下。我需要测试所有组合。即使尚未测试所有组合,只要一对与集合 ( ) 中的组合匹配,您提供的代码就会停止。
    • @cdelaura:我的代码在匹配以前使用的组合时不会停止——它会再次循环并选择另一个随机组合。我用较小的 highestNumber 值测试了我的代码,它似乎可以按我的意愿工作。如果所有组合都已尝试,我的代码将结束测验,就好像用户输入了0 的答案一样。如果你不想这样,你想发生什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多