AnandTech Forums Python help-problem with nested loops
 Register FAQ Calendar Mark Forums Read

Forums
 · Hardware and Technology · CPUs and Overclocking · Motherboards · Video Cards and Graphics · Memory and Storage · Power Supplies · Cases & Cooling · SFF, Notebooks, Pre-Built/Barebones PCs · Networking · Peripherals · General Hardware · Highly Technical · Computer Help · Home Theater PCs · Consumer Electronics · Digital and Video Cameras · Mobile Devices & Gadgets · Audio/Video & Home Theater · Software · Software for Windows · All Things Apple · *nix Software · Operating Systems · Programming · PC Gaming · Console Gaming · Distributed Computing · Security · Social · Off Topic · Politics and News · Discussion Club · Love and Relationships · The Garage · Health and Fitness · Merchandise and Shopping · For Sale/Trade · Hot Deals · Free Stuff · Contests and Sweepstakes · Black Friday 2012 · Forum Issues · Technical Forum Issues · Personal Forum Issues · Suggestion Box · Moderator Resources · Moderator Discussions
 11-13-2012, 10:58 PM #1 lemonhead71 Junior Member   Join Date: Oct 2012 Posts: 8 Python help-problem with nested loops 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```
 11-13-2012, 11:39 PM #2 postmortemIA Diamond Member     Join Date: Jul 2006 Location: Midwest USA Posts: 5,701 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. __________________ i7-3770, Intel DZ77SL-50K, VisionTek HD 7850, Dell 2707WFP, X25-M, X-Fi * PC Gamer Since 1991 *
 11-13-2012, 11:44 PM #3 lemonhead71 Junior Member   Join Date: Oct 2012 Posts: 8 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]
 11-14-2012, 12:03 AM #4 postmortemIA Diamond Member     Join Date: Jul 2006 Location: Midwest USA Posts: 5,701 you need to add another condition based on length of the picks array to exit the while loop in function lotto_add. __________________ i7-3770, Intel DZ77SL-50K, VisionTek HD 7850, Dell 2707WFP, X25-M, X-Fi * PC Gamer Since 1991 * Last edited by postmortemIA; 11-14-2012 at 12:05 AM.
11-14-2012, 12:13 AM   #5
Junior Member

Join Date: Oct 2012
Posts: 8

Quote:
 Originally Posted by postmortemIA you need to add another condition based on length of the picks array to exit the while loop in function lotto_add.
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())
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)

 11-14-2012, 08:38 AM #6 mv2devnull Senior Member   Join Date: Apr 2010 Posts: 650 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.

 Tags nested loops, python