Python help-problem with nested loops

Discussion in 'Programming' started by lemonhead71, Nov 13, 2012.

  1. lemonhead71

    lemonhead71 Junior Member

    Joined:
    Oct 31, 2012
    Messages:
    8
    Likes Received:
    0
    Hi, I'm working on this question:
    Write a program to generate at random a ticket for Lotto 6-49. This should be six numbers between 1 and 49 (remember, no duplicate numbers!). Store the numbers in a list. The program should also include an option to generate a ticket in which the user can specify some of the six numbers and the program generates the rest. The program should also include an option to generate a ticket in which the user can forbid some of the six numbers.

    I'm almost done and my problem is i'm having trouble in limiting the lotto_add and lotto_forbidden functions so that they only run a maximum of 6 times so that my lottery number list will at most contain 6 numbers as the question suggests. Thank you for your time

    Code:
     import random
    
    def lotto_add():
        picks=[]
        user_add=str(input('Would you like to specify some of the six numbers? Enter "Y" for yes and "N" for no: ' ).strip())
        while user_add=='Y' or user_add=='y': 
            user_num=int(input('Enter a number: ').strip())
            while user_num in picks:
                print('You cannot pick the same number twice. Please enter a different number: ')
                user_num=int(input('Enter a number').strip())
            user_add=str(input('Would you like to enter another number? Enter "Y" for yes and "N" for no: '))
            picks.append(user_num)
        return picks
    
    def lotto_forbidden():
        forbidden=[]
        user_forbid=str(input('Would you like to forbid some of the six numbers? Enter "Y" for yes and "N" for no: ' ).strip())
        while user_forbid=='Y' or user_forbid=='y': 
            user_num=int(input('Enter a number: ').strip())
            while user_num in forbidden:
                print('You cannot pick the same number twice. Please enter a different number: ')
                user_num=int(input('Enter a number').strip())
            user_forbid=str(input('Would you like to enter another number? Enter "Y" for yes and "N" for no: '))
            forbidden.append(user_num)
        return forbidden
    
    def lotto(picks,forbidden):
        a=6-len(picks)
        for i in range (0,a):
            num=random.randint(1,49)
            while num in picks and num in forbidden:
                num=random.randint(1,49)
            picks.append(num)
        return picks 
     
  2. postmortemIA

    postmortemIA Diamond Member

    Joined:
    Jul 11, 2006
    Messages:
    7,510
    Likes Received:
    2
    I think you can simplify code by reducing number of while and for loops to one in each function. For example, in last function you can have one while loop until you get exactly 6 numbers.
     
  3. lemonhead71

    lemonhead71 Junior Member

    Joined:
    Oct 31, 2012
    Messages:
    8
    Likes Received:
    0
    I forgot about len and tried while user_add=='Y' or user_add=='y' and len(picks)<6: and it kind of works. Here's my output:
    Would you like to specify some of the six numbers? Enter "Y" for yes and "N" for no: y
    Enter a number: 1
    Would you like to enter another number? Enter "Y" for yes and "N" for no: y
    Enter a number: 2
    Would you like to enter another number? Enter "Y" for yes and "N" for no: y
    Enter a number: 3
    Would you like to enter another number? Enter "Y" for yes and "N" for no: y
    Enter a number: 4
    Would you like to enter another number? Enter "Y" for yes and "N" for no: y
    Enter a number: 5
    Would you like to enter another number? Enter "Y" for yes and "N" for no: y
    Enter a number: 6
    Would you like to enter another number? Enter "Y" for yes and "N" for no: y <--this shouldn't be here
    [1, 2, 3, 4, 5, 6]
     
  4. postmortemIA

    postmortemIA Diamond Member

    Joined:
    Jul 11, 2006
    Messages:
    7,510
    Likes Received:
    2
    you need to add another condition based on length of the picks array to exit the while loop in function lotto_add.
     
    #4 postmortemIA, Nov 14, 2012
    Last edited: Nov 14, 2012
  5. lemonhead71

    lemonhead71 Junior Member

    Joined:
    Oct 31, 2012
    Messages:
    8
    Likes Received:
    0
    Thanks for the reply, but I'm not sure what exactly you mean.
    Code:
     def lotto_add():
        picks=[]
        user_add=str(input('Would you like to specify some of the six numbers? Enter "Y" for yes and "N" for no: ' ).strip())
        while user_add=='Y' or user_add=='y' and len(picks)<6: 
            user_num=int(input('Enter a number: ').strip())
            while user_num in picks:
                print('You cannot pick the same number twice. Please enter a different number: ')
                user_num=int(input('Enter a number').strip())
            user_add=str(input('Would you like to enter another number? Enter "Y" for yes and "N" for no: '))
            picks.append(user_num)
        print(picks)
        return picks
    the function exits after the list contains 6 values but the problem is the second last line of the output prints (Would you like to enter another number? Enter "Y" for yes and "N" for no: y <--this shouldn't be here)
     
  6. mv2devnull

    mv2devnull Senior member

    Joined:
    Apr 13, 2010
    Messages:
    943
    Likes Received:
    1
    How about thinking like this: You have at start a source list A. It has 49 items. Likewise, you have result list B and blacklist C, both empty.

    You can ask up to 6 unique numbers from the user, and add them to B. Next step is to remove from A the members of B.

    Ready to roll. Randomly pick-and-remove values from A and add to B, until B has 6 members. Done.

    It was not clear what the blacklist does (prevent selecting some numbers or decrease size of B), but either way it should be easy.