如何比较两个结果并根据匹配数量对它们进行排名

时间:2018-04-22 16:57:42

标签: python python-3.x

我是Python的初学者。 我正在制作彩票计划。我有一个有问题的部分,并提出一个问题。首先,在运行彩票计划之前,我将设置六个变量。将这些变量与彩票计划中的六个随机选择的数字进行比较。我想比较两个结果并根据匹配数对它们进行排名。

我的排名标准是:

  • 1st:匹配6个数字。
  • 第二名:5个数字匹配,另外一个数字匹配。
  • 第3名:匹配5个数字。
  • 第4名:匹配4个数字。
  • 第5名:匹配3个数字。

但它并没有像预期的那样成功。我不知道出了什么问题。请告诉我问题所在。还请告诉我如何解决它。 我希望我的问题得到了很好的解释。

提前谢谢。

## Variable declaration part ##
print("\nPlease enter the 6 numbers you expected..")
print("--------------------------------------------------------")

while True :
    a = int(input("1st expected number : "))
    if a == 0 :
        print("0 is the excluded number. Please re-enter.")
        continue
    elif a < 46 :
        break
print("Please enter a number from 1 to 45.")

while True :
    b = int(input("\2nd expected number : "))
    if b == 0 :
        print("0 is the excluded number. Please re-enter.")
        continue
    elif b == a :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif b < 46 :
        break
    print("Please enter a number from 1 to 45.")

while True :
    c = int(input("\n3rd expected number : "))
    if c == 0 :
        print("0 is the excluded number. Please re-enter.")
    elif c == a :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif c == b :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif c < 46 :
        break
    print("Please enter a number from 1 to 45.")

while True :
    d = int(input("\n4th expected number : "))
    if d == 0 :
        print("0 is the excluded number. Please re-enter.")
        continue
    elif d == a :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif d == b :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif d == c :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif d < 46 :
        break
    print("Please enter a number from 1 to 45.")

while True :
    e = int(input("\n5th expected number : "))
    if e == 0 :
        print("0 is the excluded number. Please re-enter.")
        continue
    elif e == a :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif e == b :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif e == c :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif e == d :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif e < 46 :
        break
    print("Please enter a number from 1 to 45.")

while True :
    f = int(input("\n6th expected number : "))
    if f == 0 :
        print("0 is the excluded number. Please re-enter.")
        continue
    elif f == a :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif f == b :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif f == c :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif f == d :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif f == e :
        print("Duplicate numbers. Please re-enter.")
        continue
    elif f < 46 :
        print("--------------------------------------------------------")
        break
print("Please enter a number from 1 to 45.")

## problematic part ##
winners = random.sample(range(1,41), 7)
print ("Lotto number for this week : {}, {}, {}, {}, {}, {}, and 
{}".format(*winners))
print("The winning lotto numbers this week are", "%d, %d, %d, %d, %d, %d" % 
(a, b, c, d, e, f))

if 'a, b, c, d, e, f' in winners :
    print("\nCongratulations! You are the 1st!")

if ' "%d, %d, %d, %d, %d" % (a, b, c, d, e, f)' in winners :
    print("\nCongratulations! You are 2nd!")

if ' "%d, %d, %d, %d" % (a, b, c, d, e, f)' in winners :
    print("\nCongratulations! You are 3rd!")

if ' "%d, %d, %d" % (a, b, c, d, e, f)' in winners :
    print("\nCongratulations! You are 4th!")

if ' "%d, %d" % (a, b, c, d, e, f)' in winners :
    print("\nCongratulations! You are 5th!")

else :
   print("\nNext time...!")

1 个答案:

答案 0 :(得分:1)

代码的第一部分充满了重复。太可怕了!

请注意,使用for循环和列表可以缩短程序的前102行:

print("\nPlease enter the 6 numbers you expected..")
print("--------------------------------------------------------")

numbers = []
for num in ('1st', '2nd', '3rd', '4th', '5th', '6th'):
    while True:
        n = int(input('{} expected number: '.format(num)))
        if n not in range(1,46):
            print('Please enter a number from 1 to 45.')
        elif n in numbers:
            print('Duplicate numbers. Please re-enter.')
        else:
            numbers.append(n)
            break

对于下一部分,首先我建议你以这种方式将6位获胜者与其他数字分开:

import random
*winners, additional_winner = random.sample(range(1,46), 7)
print("Lotto number for this week : {}, {}, {}, {}, {}, {}, and {}".format(*winners, additional_winner))
print("Your numbers are {}, {}, {}, {}, {}, {}".format(*numbers))

下一部分,在您的代码中出现问题,因为使用if 'a, b, c, d, e, f' in winners,您将检查字符'a, b, c, d, e, f'是否包含在winners中,这是一个整数列表,因此始终假的。

相反,使用set来检查已播放号码与中奖号码的交集,可以得到更简单的代码:

intersection = set(numbers) & set(winners)

if len(intersection) == 6:
    print("\nCongratulations! You are the 1st!")
elif len(intersection) == 5 and additional_winner in numbers:
    print("\nCongratulations! You are 2nd!")
elif len(intersection) == 5:
    print("\nCongratulations! You are 3rd!")
elif len(intersection) == 4:
    print("\nCongratulations! You are 4th!")
elif len(intersection) == 3:
    print("\nCongratulations! You are 5th!")
else:
   print("\nNext time...!")

节目输出:

  

请输入您预期的6个数字..

           

第一个预期数字:1

     

第二预期数字:2

     

第3个预期数字:3

     

第4个预期数字:4

     

第5个预期数字:5

     

第6个预期数字:6

     

本周的乐透号码:1,4,2,9,10,3和6

     

您的电话号码是1,2,3,4,5,6

     

恭喜!你是第四个!