Go Back   AnandTech Forums > Software > Programming

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
· Home and Garden
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 11-13-2012, 11:58 PM   #1
lemonhead71
Junior Member
 
Join Date: Oct 2012
Posts: 8
Default 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
lemonhead71 is offline   Reply With Quote
Old 11-14-2012, 12:39 AM   #2
postmortemIA
Diamond Member
 
postmortemIA's Avatar
 
Join Date: Jul 2006
Location: Midwest USA
Posts: 6,535
Default

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.
__________________
D1. Win7 x64 i7-3770 on Z77, HD7850, 2707WFP, 840, X-Fi D2. Win7 x64 E8400 on P35
L1. OSX 10.9 rMBP 13 L2. Vista x86 E1505
M. Galaxy S4

postmortemIA is online now   Reply With Quote
Old 11-14-2012, 12:44 AM   #3
lemonhead71
Junior Member
 
Join Date: Oct 2012
Posts: 8
Default

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]
lemonhead71 is offline   Reply With Quote
Old 11-14-2012, 01:03 AM   #4
postmortemIA
Diamond Member
 
postmortemIA's Avatar
 
Join Date: Jul 2006
Location: Midwest USA
Posts: 6,535
Default

you need to add another condition based on length of the picks array to exit the while loop in function lotto_add.
__________________
D1. Win7 x64 i7-3770 on Z77, HD7850, 2707WFP, 840, X-Fi D2. Win7 x64 E8400 on P35
L1. OSX 10.9 rMBP 13 L2. Vista x86 E1505
M. Galaxy S4


Last edited by postmortemIA; 11-14-2012 at 01:05 AM.
postmortemIA is online now   Reply With Quote
Old 11-14-2012, 01:13 AM   #5
lemonhead71
Junior Member
 
Join Date: Oct 2012
Posts: 8
Default

Quote:
Originally Posted by postmortemIA View Post
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())
    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)
lemonhead71 is offline   Reply With Quote
Old 11-14-2012, 09:38 AM   #6
mv2devnull
Senior Member
 
Join Date: Apr 2010
Posts: 787
Default

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.
mv2devnull is offline   Reply With Quote
Reply

Tags
nested loops, python

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:25 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.