Python 如何比较两个结果并根据匹配数对其进行排序

Python 如何比较两个结果并根据匹配数对其进行排序,python,python-3.x,Python,Python 3.x,我是Python的初学者。 我正在做一个彩票计划。我有一个问题,问了一个问题。首先,在运行彩票程序之前,我将设置六个变量。这些变量与彩票程序中随机选择的六个数字进行比较。我想比较两个结果,并根据匹配的数量对它们进行排名 我的排名标准是: 第一:匹配6个数字 第二:5个数字匹配,另外一个数字匹配 第三:匹配5个数字 第四:匹配4个数字 第五:匹配3个数字 但结果并没有如预期的那样。我不知道出了什么问题。请告诉我问题出在哪里。还请告诉我如何修复它。 我希望我的问题得到了很好的解释 先谢谢你 ##

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

我的排名标准是:

  • 第一:匹配6个数字
  • 第二:5个数字匹配,另外一个数字匹配
  • 第三:匹配5个数字
  • 第四:匹配4个数字
  • 第五:匹配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...!")
##变量声明部分##
打印(“\n请输入您期望的6个数字…”
打印(“-----------------------------------------------------------------”)
尽管如此:
a=int(输入(“第一个预期数字:”)
如果a==0:
打印(“0是排除的数字。请重新输入。”)
持续
elif a<46:
打破
打印(“请输入1到45之间的数字”)
尽管如此:
b=int(输入(“\第二个预期数字:”)
如果b==0:
打印(“0是排除的数字。请重新输入。”)
持续
elif b==a:
打印(“重复的数字。请重新输入。”)
持续
elif b<46:
打破
打印(“请输入1到45之间的数字”)
尽管如此:
c=int(输入(“\n第三个预期数字:”)
如果c==0:
打印(“0是排除的数字。请重新输入。”)
elif c==a:
打印(“重复的数字。请重新输入。”)
持续
elif c==b:
打印(“重复的数字。请重新输入。”)
持续
elif c<46:
打破
打印(“请输入1到45之间的数字”)
尽管如此:
d=int(输入(“\n第四个预期数字:”)
如果d==0:
打印(“0是排除的数字。请重新输入。”)
持续
elif d==a:
打印(“重复的数字。请重新输入。”)
持续
elif d==b:
打印(“重复的数字。请重新输入。”)
持续
elif d==c:
打印(“重复的数字。请重新输入。”)
持续
elif d<46:
打破
打印(“请输入1到45之间的数字”)
尽管如此:
e=int(输入(“\n5预期数字:”)
如果e==0:
打印(“0是排除的数字。请重新输入。”)
持续
elif e==a:
打印(“重复的数字。请重新输入。”)
持续
elif e==b:
打印(“重复的数字。请重新输入。”)
持续
elif e==c:
打印(“重复的数字。请重新输入。”)
持续
elif e==d:
打印(“重复的数字。请重新输入。”)
持续
如果e<46:
打破
打印(“请输入1到45之间的数字”)
尽管如此:
f=int(输入(“\n6预期数字:”)
如果f==0:
打印(“0是排除的数字。请重新输入。”)
持续
elif==a:
打印(“重复的数字。请重新输入。”)
持续
elif==b:
打印(“重复的数字。请重新输入。”)
持续
elif==c:
打印(“重复的数字。请重新输入。”)
持续
elif==d:
打印(“重复的数字。请重新输入。”)
持续
elif==e:
打印(“重复的数字。请重新输入。”)
持续
elif<46:
打印(“-----------------------------------------------------------------”)
打破
打印(“请输入1到45之间的数字”)
##有问题的部分##
获胜者=随机样本(范围(1,41),7)
打印(“本周的乐透号码:{},{},{},{},{},{},{},和
{}.格式(*winners))
打印(“本周中奖彩票号码为”、%d、%d、%d、%d、%d、%d”%d”
(a、b、c、d、e、f)
如果优胜者中的“a、b、c、d、e、f”:
打印(“\n祝贺您!您是第一个!”)
如果获奖者中的“%d,%d,%d,%d,%d”%(a、b、c、d、e、f)”:
打印(“\n合计!您是第二名!”)
如果获奖者中出现“%d,%d,%d”%(a、b、c、d、e、f)”:
打印(“\n合计!您是第三名!”)
如果获奖者中出现“%d,%d”%(a、b、c、d、e、f)”:
打印(“\n图表!您是第四名!”)
如果获奖者中的“%d,%d”%(a、b、c、d、e、f)”:
打印(“\n合计!您是第五名!”)
其他:
打印(“\n下一次…!”)

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

请注意,通过使用
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))
代码中的下一部分有一个问题,因为winners中的
如果'a,b,c,d,e,f',
您将检查字符串
'a,b,c,d,e,f'
是否包含在
winners
中,这是一个整数列表,因此总是错误的

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

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

第四名预期人数:4

第五名预期人数:5

第六名预期人数:6

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

你的号码是1,2,3,4,5,6

恭喜你!你是第四名


您得到的错误是什么?通常您会告诉我们错误,我们会帮助您修复。请重读:并尝试将问题限制在代码的某个部分。您所解释的就是我想要的解决方案。非常感谢。