如何防止用户输入两个相同的数字

时间:2017-10-24 16:09:03

标签: python

所以我在空闲时间处理我的彩票代码,我遇到了一个小问题。

我想阻止用户在他们的抽奖号码中输入两个相同的号码,即我不希望用户选择号码1两次。

其他一切正常。

这是我的整个代码:

import random
lotteryNumbers = []



print("Welcome to your new Lotto service")


lotterydrawfile = open("lotterydrawfile.txt","w")
for i in range (6):
    number = random.randint(1,60)
    lotterydrawfile.write(str(number) + " ")


lotterydrawfile.close()


userentryfile = open("userentryfile.txt","w")
for i in range (6):
    number = input("Enter a number between 0 and 60. Numbers must not be in word format")
    for i in range (1000):
        if number < "0" or number > "60":
            print("Your number needs to be between 0 and 60")
            number = input("Enter a number between 0 and 60")

    print(str(number))
    userentryfile.write(str(number) + " ")
userentryfile.close()


userlottonumbers = open("userentryfile.txt").read()

usernumber = []
number_string = ""
for i in userlottonumbers:
    number_string = number_string + str(i)

    if str(i) == " ":
        usernumber.append(number_string)
        number_string = ""
print("Your numbers are " + str(usernumber))


lotterydraw = open("lotterydrawfile.txt").read()
number_string1 = ""
lottodraw = []
for j in lotterydraw:
    number_string1 = number_string1 + str(j)

    if str(j) == " ":
        lottodraw.append(number_string1)
        number_string1 = ""

print("The lotto draw was " + str(lottodraw))

same = 0
for i in usernumber:

    for j in lottodraw:

        if i == j:
            same =same + 1

prize = same * 100

if same == 6:
    prize = 10000000
print("You won £" + str(prize))

我想要改进的是行开始的地方:

    userentryfile = open("userentryfile.txt","w")
for i in range (6):
    number = input("Enter a number between 0 and 60. Numbers must not be in word format")
    for i in range (1000):
if number < "0" or number > "60":
        print("Your number needs to be between 0 and 60")
        number = input("Enter a number between 0 and 60")

但在下面,我希望用户停止输入两个相同的数字。

提前致谢。

3 个答案:

答案 0 :(得分:1)

将用户输入的数字保留在列表中并进行测试以查看该列表中是否已有最新输入:

numbers = []

if number not in numbers:
    numbers.append(number)
else:
    # "each number must be unique..."

答案 1 :(得分:0)

python中有一个名为set的数据类型。放入集合中的所有内容都必须是唯一的。一次只能有一个集合中的每个东西。以下代码也应该捕获大多数(如果不是所有)将来会导致问题的边缘情况。它确保用户输入确实是一个数字,实际上在0到60之间,之前没有被选中,并且将从用户获得正好6个数字。

def get_user_numbers(number_of_guesses):

    # a set can only have one element with the any given value
    users_numbers = set()

    # used to keep the while loop going until you have all the information you want.
    got_all_numbers = False
    while not got_all_numbers:

        # tries to get input from the user. The except statement will be hit if the
        # user gives you something other than a whole number.
        try:
            current_number = int(raw_input("Enter a number between 0 and 60"))
        except ValueError:
            print 'Please enter a number'

        # Checks to make sure the number is in the range you want it to be.
        if current_number < 0 or current_number > 60:
            print 'Please pick a number between 0 and 60.'
        # Checks to see if the number has already been used.
        elif current_number in users_numbers:
            print 'Please pick a number that you have not already used'
        # if everything else if correct it will add the users number to the set.
        else:
            users_numbers.add(current_number)
        # Checks to make sure the set still has less than 6 numbers in it. If it
        # does then the while loop will keep going until there are 6 guesses
        if len(users_numbers) == number_of_guesses:
            got_all_numbers = True
        elif len(users_numbers) > number_of_guesses:
            # There is a big problem... The user managed to give you more guesses
            # than you are asking for! The program should exit or you should start
            #  over with blank guesses
            raise ValueError
        # If you wanted to you could print out all of the users guesses here so they
        # can see what they have already tried

    return users_numbers


print get_user_numbers(6)

要在程序中使用此功能,只需将函数调用放在文件的顶部,然后在已编写的代码中使用函数调用。以下是您的代码的示例:

# -*- coding: UTF-8 -*-
import random


def get_user_numbers(number_of_guesses):
    users_numbers = list()
    got_all_numbers = False
    while not got_all_numbers:
        try:
            current_number = int(raw_input("Enter a number between 0 and 60:  "))
        except ValueError:
            print 'Please enter a number'
        else:
            if current_number < 0 or current_number > 60:
                print 'Please pick a number between 0 and 60.'
            elif current_number in users_numbers:
                print 'Please pick a number that you have not already used'
            else:
                users_numbers.append(current_number)
            if len(users_numbers) == number_of_guesses:
                got_all_numbers = True
            elif len(users_numbers) > number_of_guesses:
                raise ValueError
    user_numbers_string = ' '.join(str(guess) for guess in users_numbers)
    return user_numbers_string


lotteryNumbers = []

print("Welcome to your new Lotto service")

lotterydrawfile = open("lotterydrawfile.txt", "w")
for i in range(6):
    number = random.randint(1, 60)
    lotterydrawfile.write(str(number) + " ")

lotterydrawfile.close()

# Here is where you call the function that is at the top of the file 
userlottonumbers = get_user_numbers(6)

usernumber = []
number_string = ""
for i in userlottonumbers:
    number_string = number_string + str(i)

    if str(i) == " ":
        usernumber.append(number_string)
        number_string = ""
print("Your numbers are " + str(usernumber))

lotterydraw = open("lotterydrawfile.txt").read()
number_string1 = ""
lottodraw = []
for j in lotterydraw:
    number_string1 = number_string1 + str(j)

    if str(j) == " ":
        lottodraw.append(number_string1)
        number_string1 = ""

print("The lotto draw was " + str(lottodraw))

same = 0
for i in usernumber:

    for j in lottodraw:

        if i == j:
            same = same + 1

prize = same * 100

if same == 6:
    prize = 10000000
print("You won £" + str(prize))

答案 2 :(得分:0)

我认为你真的想要一个while循环并存储拾取后的数字,如下所示:

    numbers = []
    while len(numbers) < 6:
    number = input("Enter a number between 0 and 60. Numbers must not be in word format")
    if not number.isdigit():
      print("Your number is not in number format")
    elif float(number) < 0 or float(number) > 60:
      print("Your number needs to be between 0 and 60")
    elif number in numbers:
      print("You already picked that number")
    else:
      numbers.append(number)

这在此处进行了测试:https://repl.it/NJWE/2