彩票程序,让用户选择他们希望一次玩多少次

2024-04-18 21:12:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个彩票程序,可以在用户输入号码后输出结果。但我想让用户也能选择“他们玩了多少周”,也就是说,程序输出随机结果的次数。基本上使用他们输入的号码玩多个相同号码的彩票游戏x他们希望的次数。我想知道如何根据他们希望玩的次数重复我的功能

这是我的不完整代码

 import random


NUMBER_OF_PICKS = 3
MINIMUM_SELECTION = 1
MAXIMUM_SELECTION = 36
MONEY_WON = 10000
OFFSETT = 4

USER = input("Please enter your name:")
print("Hi " + USER + " good luck ")
WEEKS_PLAYED = input("How many weeks do you want to play: ")



def verify(playerNumbers, winningNumbers):
    if playerNumbers == winningNumbers:
        print("Congratulations! You Win ${}!".format(MONEY_WON))
        print("Your numbers: ", playerNumbers, )
        print("The winning lottery numbers were: ", winningNumbers)

    else:

        print("Sorry, you lose...")
        print("Your numbers: ", playerNumbers)
        print("The winning lottery numbers were: ", winningNumbers)


# 'get_user_nums', gets user numbers and puts into a sorted list for x in WEEKS_PLAYED:
 def get_user_nums():
    user_nums = []
    while len(user_nums) < NUMBER_OF_PICKS:
        nums = input("Pick a number {} through {}: ".format(MINIMUM_SELECTION, MAXIMUM_SELECTION))
        try:
            nums = int(nums)
        except:
            print("Sorry your input must be an integer!")
            continue
        if MINIMUM_SELECTION <= nums <= MAXIMUM_SELECTION:
            if nums not in user_nums:
                user_nums.append(nums)
            else:
                print("Sorry, you have already inputted that number")
        else:
            print("Sorry, Your number was not in range")

    return sorted(user_nums)


# 'get_winning_nums', creates a sorted list with random nums ranging from 0-9 with a range of 3 values
def get_winning_nums():
    return sorted(random.sample(range(MINIMUM_SELECTION, MAXIMUM_SELECTION), NUMBER_OF_PICKS))


# 'menu', creates the main menu to choose game or exit program


def play_pick_n():
    user_nums = get_user_nums()
    winning_nums = get_winning_nums()
    verify(user_nums, winning_nums)


# 'main', calls the other functions
def main():
    # lottery_menu()
    while True:
        choice = input("\nPlay?: Yes or No: ")
        if choice == 'Yes':
            string = "\n[Play Pick {}]".format(NUMBER_OF_PICKS) + "selected!"
            dotted = '\n' + len(string) * "-"
   
            print(dotted, string, dotted)

            play_pick_n()
            break

        elif choice == 'No':
            print("Thanks for playing!\n")
            break

        print("Sorry, that is not a valid input. \nPlease enter either Yes or No")


if __name__ == '__main__':
    main()

谢谢你的帮助


Tags: ofnumberinputgetifmaindefprint
1条回答
网友
1楼 · 发布于 2024-04-18 21:12:59

如果您希望在所有周内使用相同的数字,请使用:

user_nums = get_user_nums()

for week in range(0, WEEKS_PLAYED):
    winning_nums = get_winning_nums()
    verify(user_nums, winning_nums)

您可能想在play_pick_n函数中移动周数的问题,这样玩家就可以决定每一组数字应该运行多长时间

相关问题 更多 >