Python使用random.randint防止数字被选中两次

时间:2016-12-24 19:16:00

标签: python python-3.x

我刚开始精益python并且作为一个额外的功劳,我已经制作了一个代码,它将用户设定的数量从0随机打印到用户设置的最大数量。我想知道如何保持一个数字被选中两次。我很乐意得到任何人的帮助。

import random
#Set up loop incase user does not like answer 
again = 10
while (again > 0):
    #user parameters
    Num_winners=int(input("Enter how many winners you want: "))
    max_number=int(input("Enter the hightst number in range: "))
    # Add blanck space in log
    for _ in range(5):
        print("")

    #Counts times ran
    times_ran = 1

   #picks winner
    for i in range(Num_winners):
        num = random.randint(0,max_number)
        print("Winner Number %d is: %d"  % (times_ran, num))
        times_ran += 1
    print("")
    again -= 1

    #Ask to do again
    x = 0
    while (x == 0): 
        print("Do you want to run again?")
        choice = input("Yes/No: ").lower()
        print(choice)
        if choice == 'yes':
            again = 10
            x = 1
        elif choice == 'no':
        again = 0
        x = 1
        elif choice == 'y':
            again = 10
            x = 1
        elif choice == 'n':
            again = 0
            x = 1
        else:
            print("")
            print("Error: invalid imput;")
            print("      Try again")
            x = 0
            print("")

0 个答案:

没有答案